Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C with structs or C++ with classes... Your choice and why.

This thread is locked; no one can reply to it. rss feed Print
C with structs or C++ with classes... Your choice and why.
_Dante
Member #7,398
June 2006
avatar

Shawn Hargreaves said:

How on earth did we end up talking about snake fetishes? I can think of a lot of things you could do with a snake, with or without weak and/or shared pointers, but I don't think I'd particularly enjoy any of them...

Leather, on the other hand, now that's something I can relate to.

Sheesh, no imagination - what's wrong with snakeskin leather?

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Andrei Ellman
Member #3,434
April 2003

Steve++ said:

Quote:

Unless you're working on an existing C project. Converting an entire project from C to C++ (even if it's object-oriented C) can take so much time that it would be more productive to use OO C.

So you spend half a man hour renaming everything from .c to .cpp and compiling everything, casting any void pointers that the compiler may complain about. Is that half a man hour such a big price to pay for having language supported OOP at your disposal instead of doing it the hard way in pure C?

Argh!" I should have made myself clearer. I meant to say converting a C project to use C++'s fancy new features. If I were to add some polymorphic virtual abstract base classes and all that, I'd either have to write C++ wrappers for my C 'classes' (and hope that the compiler optimises it away), or convert any code that resembles what I posted previously in this thread to use a more C++ like approach ( = major rewrite). Or I could just hope that the new C++-ish stuff only interacts minimally with the existing code.

_Dante said:

what's wrong with snakeskin leather?

It can't be cast into anything.

AE.

--
Don't let the illegitimates turn you into carbon.

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

I'll pay you $100 to port Allegro to C++. As I'm generous I'll give you a whole day to do it :)

Reread his post. By his argument, Allegro is already C++. I mean, I've been using it with C++ for years ...

BTW, this should be common knowledge, but my goatee owns all your beards, reigning supreme on these forums for a year .... mua ha ha!

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Onewing
Member #6,152
August 2005
avatar

The problem with beards is there are too many ways of going about them, making them hard to maintain in the work environment. Plus, you have to rely on lowlevel methods of testosterone to produce proficient scruffs. Some people end up with mixed patches that just plain look ugly, not an impressing office factor.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Steve++
Member #1,816
January 2002

Quote:

Some/Most times the Nookie interface itself incurs huge costs all by itself.

My Nookie Provider suggested I apply the useGillette() method to her Nookie interface.

_Dante
Member #7,398
June 2006
avatar

Quote:

Quote:

Some/Most times the Nookie interface itself incurs huge costs all by itself.

My Nookie Provider suggested I apply the useGillette() method to her Nookie interface.

That's a brave Provider you've got there, as the useGillette() method is generally considered "unsafe" as it does no bounds checking, and can actually damage the underlying hardware. Might I suggest a Wax pattern, and in particular the latest version coming out of Brazil.

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Steve++
Member #1,816
January 2002

;D

Back on topic...

Lately I discovered that table-driven programming is a great 'alternative' to procedural or OOP programming. I used quotes around alternative because it can be used in a procedural or OOP environment.

I recently started to develop an asynchronous version of MooServer (server for a Multimedia Fusion plugin that allows online play) using Java. There are sockets, channels, connections, sessions, servers, players, etc. that can each be modelled with a class. So I started writing a class for each one, then the methods to make them interact. I actually found that all this OOP was getting in the way. My code was becoming spaghetti-like.

So I stepped back and thought, what would all this information look like in a relational database? So I designed some tables on paper and for the first time, I completely understood the relationships between all the data. So I coded everything in one class (MooServer) using tables to map all the data. The whole thing became so much simpler. The only reason I didn't finish this project (which would have taken about three full days) is that all this mundane life stuff came up that needed my attention - the same reason every project dies, but that's another story...

Here's a tiny snippet:

1package com.atulos.mooserver;
2 
3import java.util.*;
4 
5public class MooServer
6{
7 private SetTable<Integer, Integer> connectionTable;
8 private SetTable<Integer, Integer> sessionTable;
9 private BidirectionalMap<Integer, String> sessionNaming;
10 private Hashtable<Integer, String> connectionNames;
11 private static int nextSessionId = 0;
12 
13 // Get the name of the connection
14 public String getConnectionName(int connectionId)
15 {
16 return connectionNames.get(connectionId);
17 }
18 
19 // Get the name of the session
20 public String getSessionName(int sessionId)
21 {
22 return sessionNaming.getRight(sessionId);
23 }

The moral of the story is that if you have a clue about how to model information in a relational database and you have mapping data structures available, you should use OOP only to serve table-driven programming because table-driven programming absolutely rocks.

EDIT:
The problem with OOP is that, although it is nice for modelling individual types of real-world objects, there are no standards (besides is-a and has-a) for defining relationships between classes. It is left to the programmer to implement these relationships, without the opportunity to declare them in a standard way.

I suppose table-driven programming could be (and probably has been) implemented in plain old C quite effectively.

HoHo
Member #4,534
April 2004
avatar

Quote:

Reread his post. By his argument, Allegro is already C++. I mean, I've been using it with C++ for years ...

Not exactly. Allegro doesn't compile with C++ compiler and I understood that he meant making a C project compile as C++ one.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

nonnus29
Member #2,606
August 2002
avatar

Quote:

you should use OOP only to serve table-driven programming because table-driven programming absolutely rocks.

Your formulation of 'table driven programming' is a bit beardless, err weak. Real table oriented programming requires that the executable code also live in tables. That could be done in java or c# using dynamic class loading but I've not seen anyone do that.

I completely agree with you that oop is unsuited for many problems, for example my problem of easily creating a web application of 500 forms with simple to complex business logic. I think it was Paul Graham in on of his lisp articles who made the observation that business appiclations are basically a series of a arbitrary exceptions to general rules. Sure some things like customer management (name, address, account information) can be easily implemented in with oop or whatever, but when you get to bizarre stuff like handling the record keeping for variable rate annuities or tax regulations, oop falls flat on its face. Which is why in 2006 cobol is thriving.

BrknPhoenix
Member #7,304
June 2006

C++, and here's why:

1) I find the code to be more readable. Done correctly, it's nigh impossible to not be able to easily locate the 'hooks' in your program.
2) I like my code to be as neat and structured as possible. The flexibility of a class far outweighs that of a struct when it comes to OOP.
3) It's more efficient in terms of time spent coding.
4) Reusability. I'm able to use many of my classes from program to program without having to make any changes to them at all.
5) They're fun!

Steve++
Member #1,816
January 2002

Quote:

Real table oriented programming requires that the executable code also live in tables.

I don't think table oriented/driven programming has been formalised to the extent that you can say what is real and what isn't, except that it should make heavy use of tables. My personal flavour of TDP involves storing all the data in tables and programming the relationships between them in a procedural manner. The Java collections API supports that rather nicely. To me, putting code in tables sounds like it could me a nice trick, but it would probably obstruct code readability and it seems like a way of using tables to achieve OOP (sounds like Lua).

Quote:

That could be done in java or c# using dynamic class loading but I've not seen anyone do that.

Interestingly, I've done some dynamic class loading in Java. The purpose was to use Java as a scripting language in a Java MMORPG. I had it working so NPCs could be hot-swapped without changing their class names.

axilmar
Member #1,204
April 2001

Quote:

I suppose table-driven programming could be (and probably has been) implemented in plain old C quite effectively.

Here is a constructive critisism about OO and tables.

Lucid Nightmare
Member #5,982
July 2005
avatar

Well, its more of an individual likingness and comfortability factor with the two languages...

I like C++ over C and normally code only in C++ whenever I have to. It actually depends from a person to person...

Its not necessary for anyone of them them being better or superior but just a matter of choice...

Well, its just an opinion of mine and I might be wrong...

Click Here For My Website

Two Golden Rules of life- Firstly, I'm always right and secondly, if you think otherwise, slap your face and read rule number one again!

Neil Walker
Member #210
April 2000
avatar

I'm still wondering what shared pointers has to do with beards.

However, the mystery that troubles me is why women in IT are so ugly. It's as if, in order to embrace technology you have to be a man or a minger.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

_Dante
Member #7,398
June 2006
avatar

Quote:

I'm still wondering what shared pointers has to do with beards.

It's pretty simple, really. I have a beard and shared pointers don't. See how that works?

Quote:

However, the mystery that troubles me is why women in IT are so ugly.

I'm curious as to why you think the men in IT aren't. Attractive people work in sales and marketing...

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Richard Phipps
Member #1,632
November 2001
avatar

I use C with some extras provided by C++ (i.e. use of bools, and the STL library).
I've always programmed in a procedural way and am very happy with it! :)

Neil Walker
Member #210
April 2000
avatar

Quote:

I'm curious as to why you think the men in IT aren't. Attractive people work in sales and marketing...

I work in IT and (non-IT) women adore me. However I've seen Richard Phipps, which seems to correlate your statement ;)

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Richard Phipps
Member #1,632
November 2001
avatar

Quote:

I work in IT and (non-IT) women adore me.

I almost choked on my dinner! :D

Sniff.. I was having a bad hair day (or two) when those pictures were taken. :P

_Dante
Member #7,398
June 2006
avatar

Quote:

I work in IT and (non-IT) women adore me.

Your mom telling you how handsome you look in your tux on prom night doesn't count.

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

23yrold3yrold
Member #1,134
March 2001
avatar

I work at a tech support center. Dunno if that counts as IT, but a lot of the more knowledgable girls on the floor are really cute. :o Most of the guys are fat and ugly and we outnumber the girls 5 to 1; dunno how that fits into your theory though. :)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

_Dante
Member #7,398
June 2006
avatar

Pretty much confirms mine ;)

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

ReyBrujo
Moderator
January 2001
avatar

Quote:

And why anyone would want to use/have a weak_pointer<> is beyond me.

Ah, I miss that innocence ;D That is the bad thing about programming under Windows :'(

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Ah, I miss that innocence ;D

Oh I love "technical" weak pointers, but I think a few people have missed the not so hidden joke ;)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

ReyBrujo
Moderator
January 2001
avatar

:-[

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Samuel Henderson
Member #3,757
August 2003
avatar

23 said:

I work at a tech support center. Dunno if that counts as IT, but a lot of the more knowledgable girls on the floor are really cute. :o Most of the guys are fat and ugly and we outnumber the girls 5 to 1; dunno how that fits into your theory though. :)

Wow. Where I work (also tech support), all the tech saavy women are simple technicians, and all the beautiful (but technically challenged) women have cushy overloard type supervisor jobs.

Anyways, back on topic. I prefer C++ because that was all we learned in college thus far (aside from JavaScript, Java, MySQL, and VB.NET).

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.



Go to: