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.
Jonatan Hedborg
Member #4,886
July 2004
avatar

What is this, the fourth C vs C++ thread this week?

Jonny Cook
Member #4,055
November 2003

Quote:

What is this, the fourth C vs C++ thread this week?

Something like that.

Quote:

Some can prefer C, other C++,pascal,basic,C#... I personally prefer C. And so ? You can not live knowing that other don't use the same tool than you ? Héhé.

Not sure who that question was aimed at, but I'd like to answer it anyway. :P
I really don't care what other people use. If they want to use C, that's fine. C++ is fine as well. Hell, if you want to use BASIC you can. Te me it just seems a little dumb to spend all this time trying to emulate useful features in one language when they have already been fully implemented into another, almost identical language.

Plus, at the end of the day, what's wrong with being a little lazy? If I (in a general sense) can produce as good or better results in much less time, what's there to be ashamed of?

The face of a child can say it all, especially the mouth part of the face.

GullRaDriel
Member #3,861
September 2003
avatar

I do not say we should not have do this thread .

EDIT: Jonny Cook : Héhé ;-)

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Jonny Cook
Member #4,055
November 2003

That makes two of us... ;)

The face of a child can say it all, especially the mouth part of the face.

Andrei Ellman
Member #3,434
April 2003

Steve++ said:

If you find yourself making C do C++ things, then perhaps it's easier to use C++.

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. But I agree that if you like OOP, then you should start new projects in C++.

Jonny Cook said:

That's a great philosophy you've got there. ::) "Always do it the hard way!"

I have a syaing in life. If it isn't worth doing the hard way, it's not worth doing at all.

AE.

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

vixinu
Member #7,343
June 2006
avatar

Agreed

_Dante
Member #7,398
June 2006
avatar

Quote:

The truth is the C++ gives compiler warnings potentially where C doesn't, but it compiles the code anyway.

There are certain cases where my compiler requires a cast. For instance, assigning a void pointer to anything but another void pointer is not allowed:

g++
error: invalid conversion from `void*' to `TestStruct*'

msvc
error C2440: 'initializing' : cannot convert from 'void *' to 'TestStruct*'

armcpp
Conversion from 'void*' to pointer to non-'void' requires an explicit cast

That notwithstanding, I still agree with Steve++ in that the few restrictions the language itself puts on you are actually a good thing (think type safety), and in the end you do what you need to do quickly and cleanly.

I don't know how many ways I can say this, but I'll try one last time. Writing in C++ doesn't require any OOP. Or templates. Or streams, or MIL's, or feature X. It has lots of features that make OOP easier - much easier - than a language not designed for it, but it's still essentially C with extra stuff. Use the stuff you like, ignore the stuff you don't.

Quote:

I have a syaing in life. If it isn't worth doing the hard way, it's not worth doing at all.

I have one, too (shamelessly stolen from Three Dead Trolls in a Baggie):
"Yeah, but I've got a girlfriend, and things to get done."

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

nonnus29
Member #2,606
August 2002
avatar

Quote:

Calling a particular function in a switch statement isn't polymorphism, it's essentially an if-else construction.

And what do you think the compiler is doing behind the scenes? ie 'late binding'?

And I'd also point out that c++ wasn't the first object oriented language base on c, objective c has a completely different way of implementing oop on top of c. I'd recommend looking at it if you (all) haven't, it gives you a nice perspective.

Paul Pridham
Member #250
April 2000
avatar

Dante: Nobody said anything about C needing case statements for OOP. Not that using them isn't a valid way to do OOP stuff, because you can certainly do it that way. However, I usually use a vtable, or "interface" (struct of function pointers). Polymorphism can come from calling through a different, but compatible interface. Or, it can come from using a different set of functions to operate on the functionally compatible data structure (through casting). Really, using function pointers, you can come up with all sorts of different OOP schemes. Prototype an object by copying it (along with function pointers) and then changing the pointers or data pointed to by the new object. Etc. This kind of thing isn't written into either language, but C++ directs you to a certain kind of OOP usage, while C does not. Earlier, I implied that C++ was dogmatically restrictive. Sure, you can write code any way you want in C++, the "superset" of C. It just tends towards certain trends and design patterns.

C++ does a lot of things implicitly, and C explicitly. I prefer explicit control when it comes to language features and behaviour. C makes less assumptions, leaving me free to make my own. Of course, if all of these "languages" just had a Forth precompiler, we wouldn't be having this discussion. ;)

And yes, aren't we all sick of this debate already... :-*

_Dante
Member #7,398
June 2006
avatar

Quote:

And what do you think the compiler is doing behind the scenes? ie 'late binding'?

Don't know, don't care. That's the point - the compiler can do what it likes, as long as it's compliant.

Quote:

Nobody said anything about C needing case statements for OOP. Not that using them isn't a valid way to do OOP stuff, because you can certainly do it that way.

Nobody said needing, fair enough. But suggesting that a series of switch statements constitutes polymorphism? Any language with an if/else construct can do that. I'm gonna have to strongly disagree.

The person posing the original question (remember him?) stated that he was new to the language. You can't seriously suggest that he should attempt to implement all sorts of complicated features when C++ gives it to him for free. The question was, are there any advantages to using one over the other? The advantage of using C++ is that it has all the features of both languages, so you have lots of choices. The advantage of using C is... some perceived control that apparently isn't available in C++.

I can't imagine what explicit control buys you, but whatever makes you sleep better.

Quote:

And yes, aren't we all sick of this debate already... :-*

Totally. But as everybody knows, the most important thing is to be right (or at least perceived right), which means one can't possibly let the other guy have the last word ;D

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

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

I can't imagine what explicit control buys you

Peace of mind. And control. Its all about control.

--
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

Bob
Free Market Evangelist
September 2000
avatar

Quote:

I can't imagine what explicit control buys you, but whatever makes you sleep better.

Some constructs just can't be expressed be plain direct-hierarchy OOP. You really want to custom-build classes. You could switch to another language which allows it, or you can just manually build a vtable, C style.

--
- Bob
[ -- All my signature links are 404 -- ]

Archon
Member #4,195
January 2004
avatar

I use C++ and for cleanliness reasons, I use structs for grouping data (sound as a 'point' or 'vertex') and classes for interactable objects - be like me.

Goalie Ca
Member #2,579
July 2002
avatar

If using c++ then i will make full use of classes rather than structs.

this

class Array<T,int dim>
{
   typedef Array<T,dim> array;
    T[dim];
 public:
    Array(const array& copy);
    T& operator[](size_t index);
    operator=(const array& assign);
    bool operator==(const array& equals);
    bool operator!=(const array& notEquals);
    swap(array& swapWithMe);

    size_t size(); //returns "dim". 
};

is certainly a lot easier to worth with than just a straight array. Since you are talking about structs vs classes image a case where you were dealing with strings

typedef struct string
{
   char* dat;
   size_t length;
} string;

or

1class string
2{
3 char* dat;
4 size_t length;
5public:
6 string( const string& a ) //copy
7 /* more useful constructors */
8
9 trim(), regex(), //other shit too
10 
11 //various operators
12 operator=
13 operator==
14 operator>
15 operator[]
16
17 const char* c_str(); //
18}

Both have the same memory layout (this is a POD class, aka plain ol' data). The memory layout would change if you have virtual (a table would be needed).

-------------
Bah weep granah weep nini bong!

A J
Member #3,025
December 2002
avatar

C is for people that think beards are cool.

___________________________
The more you talk, the more AJ is right. - ML

GullRaDriel
Member #3,861
September 2003
avatar

And AJ is a name for Big Baby Boys ! ;-D

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

I never said it was a language-specific feature, I said C wasn't fully capable of it, and it isn't.

You said:

Quote:

You can't do OOP in C

which is 100% false, no matter how you look at it. You can very well do OOP in C. As a language, C may not be designed with OOP in mind, but there is nothing standing in your way to take a fully OOP approach with programs written in C. It may just take a little extra grunt work. Inheretence, encapsulation, and polymorphism are not required for OOP, they simply make it easier.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

BAF
Member #2,981
December 2002
avatar

Quote:

But it's your own personal preference. I prefer C because it's simpler, and you can ignore all that "using namespace std" crap.

That shows that you know nothing about C++. ;) the std namespace is an API, it has nothing to do with C++ itself.

Quote:

More readable code? What a load of crap. They're virtually the same for readability.

At times, C++ can be more readable (if you're the one writing the code). But if you look at some C++ that uses all kinds of C++'s features, and you didn't write it, you can spend hours trying to understand what it does. ;)

_Dante
Member #7,398
June 2006
avatar

Quote:

Inheretence, encapsulation, and polymorphism are not required for OOP, they simply make it easier.

You had me right up to this statement. I take back the "C can't do OOP" part, I've been shown the error of my ways, but what I really meant to say originally was basically that C isn't really an OO language - and I still stand behind that. The facilities to do OOP are not an inherent part of the language. Being able to accomplish OOP with it doesn't make it an OO language any more than strapping a penguin to a rocket makes it able to fly.

All that aside, the claim that inheritance, encapsulation, and polymorphism are not required for OOP is simply incorrect. These things aren't suggested features, they're part of the definition.

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

Steve++
Member #1,816
January 2002

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?

Like the others here that are actually speaking sense, I'm not supporting C++ over C or vice versa. All I'm saying is that if you want to do OOP, use a language that natively supports a reasonably large subset of the paradigm. Almost all C++ programmers are aware they could do OOP in C if they wanted. Big deal.

C is a good procedural language and I will use it when I have a good enough reason to. There are times when I don't particularly care for C++'s strict type checking.

Paul Pridham
Member #250
April 2000
avatar

Quote:

But suggesting that a series of switch statements constitutes polymorphism? Any language with an if/else construct can do that. I'm gonna have to strongly disagree.

Sure. You can implement polymorphism using switch/case or if/else, using run-time type information and delegation, for example. I'm not saying it's pretty, but it's certainly possible. You can disagree if you want, but you're still wrong.

Quote:

The person posing the original question (remember him?) stated that he was new to the language. You can't seriously suggest that he should attempt to implement all sorts of complicated features when C++ gives it to him for free.

No. I don't suggest that he implements anything complicated... far from it. I suggest that the C programming mindset produces simpler code by eliminating many potentially misleading language/design pitfalls, and that C++-style OOP is possible if you want it. As well, you can produce "object-based" programs that doesn't need to adhere to the full compliment of OOP dogma too, which again can simplify things.

His original question was whether to use structs or classes. Well, I say structs. And so...

David_at_wedu: Keep it simple, and don't get distracted with overdesign and language purity. This is just my opinion, though, and it's evident that everybody has one of their own. :)

HoHo
Member #4,534
April 2004
avatar

Quote:

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?

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

__________
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

Shawn Hargreaves
The Progenitor
April 2000
avatar

I actually find this whole debate pretty funny.

You're all arguing as if C++ is a pure OO language, while C is a more flexible language that you could perhaps implement OO inside.

Not true! C++ is a multiparadigm language. So is C, but a far simpler one. A more meaningful debate would be between small and simple versus bloated and powerful.

Of course it is true that you can do OO in C, but it is also totally true that you can write procedural or functional code in C++. And that's no accident: C++ was designed to be a big tent, not a pure OO language. In fact it has at least as many features geared toward functional programming as it does to polymorphic class hierarchies, as explained by the father of the whole thing:

http://www.research.att.com/~bs/ieee_interview.html
http://linux.sys-con.com/read/32929.htm

If you want a pure OO language, try here:

http://c2.com/cgi/wiki?SmalltalkLanguage

(or arguably Java / Ruby)

James Stanley
Member #7,275
May 2006
avatar

I used to use C until I discovered vectors. Now I use C++ purely because I don't have to bother with all this malloc() crap. But, I also do the bad programming practice (as I've heard it called somewhere) of 'using C++ as a better C'. To me there's nothing wrong with that. I'd use C if it had an equivalent of vectors.

Shawn Hargreaves
The Progenitor
April 2000
avatar

That's not a bad programming practice at all! The man himself (Stroustrup) encourages a mixed paradigm programming style, using whatever features make sense for you.

That is simultaneously the greatest strength and the greatest weakness of C++.

A strength because it gives huge flexibility. Don't like exceptions? You don't have to use them. Don't like the standard memory allocator? You can replace it. Don't like virtual methods? They're optional. You can use it like Smalltalk, or as a better C, or as a compile time metaprogramming language, or a million things in between.

But a weakness because everyone ends up using it in a slightly different way. That makes it really hard to reuse third party C++ libraries, because the chances are they're returning error codes while you are using exceptions, and their memory allocator is different to yours.

That flexibility is a problem in C, too, because it is also a very flexible multiparadigm language. Flexibility is good until you want to reuse code, which is a large part of why Java and C# lock things down a bit more tightly (thou shalt use garbage collection, and thou shalt use exceptions to report errors, and thou shalt use the builtin string type, not one you just made up yourself, and thus shall all thine libraries play nicely together).



Go to: