• Advertisement

Archived

This topic is now archived and is closed to further replies.

Structs vs Classes in C++

This topic is 5098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know two differences between structs and classes: -structs date from C but were improved in C++ -classes are new, and while in structs everything is public by default, in classes it isn't. Are there any other differences than these? Is one of them faster than the other? Is there a reason to use one or the other, are both useful for something or is it better to always use classes? Also, another question (kinda newbie): what's the difference between the "." and "->" symbol in C++? [edited by - Boops on March 5, 2004 7:21:41 PM]

Share this post


Link to post
Share on other sites
Advertisement
One more difference off the top of my head: inheritance with structs is public by default, but private by default with classes.

. is used with the actual objects or references.

-> is used with pointers.

Share this post


Link to post
Share on other sites
structs and classes are EXACTLY the same, except that the default access type is public in structs, and private in classes. So the follwing two are exactly the same:
class Foo {public: int i};
struct Foo {int i};

Theres no other differences - inheritance works the same, member functions work the same, etc etc.

However, since struct is from C, people tend to use a struct to represent a general data type without member functions (or maybe a constructor / destructor).

Share this post


Link to post
Share on other sites
The one and ONLY difference between structs and classes:

Members of structs are public by default.
Members of classes are private be default.

Although many people choose to impose arbitrary restrictions on them; for example, I never add functions to structs, only classes. Ctor/dtor maybe, but no member functions.

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Steve
Theres no other differences - inheritance works the same, member functions work the same, etc etc.


Inheritance does not work the same. I''ll say it again: Inheritance for structs is public by default. Inheritance for classes is private by default.

Share this post


Link to post
Share on other sites
Yup, so pretty much the only reason to pick one over the other is style.

Coders generally use structs for things that are inert, they store data but they don''t do anything, all their members are public, they don''t have functions (or maybe they have a couple accessor functions), and they can be safely shallow copied.

Classes are generally used for objects that actually do something, they have private data, and they have functions that perform actions.

Whether or not you want to follow that distinction is completely up to you, but that''s the way it''s generally done.

Share this post


Link to post
Share on other sites
If you''re constantly switching between C and C++ you might find it easier to just use structs.

Share this post


Link to post
Share on other sites
I have found that forward declarations in MSVC++ have to match types so if something is a struct and you forward declare it as a class it won''t allow it. To be consistent and not have to look things up to see whether they are a class or a struct I make everything classes. I know the compiler catches it but you''ll end up with a list of forward declarations like this.

class Token;
struct Foo;
struct Bar;
class Generaliser;
class Opener;
class CanOpener;
class BottleOpener;
struct pointerDeleter;

etc.

Share this post


Link to post
Share on other sites
quote:
Original post by commander keen
why do people repeate what others have already posted...lol


This isn''t the question that sticks out in my mind. The question that sticks out in my mind is this:

With all these moderators on the board, whom I actually respect most of their opinions ( woefully rare in the forum world ), why are these kinds of posts made all the time? Why aren''t there literally stickies with all the frequently asked questions and automatic deletion of threads that are answered in said stickies?

It seems it would clean the board up tremendously in a lot of the sections, make the board smoother, take up less space and possibly make searches return better results, etc.

Anyway, sorry for highjacking the thread.. it''s just been on my mind a lot when I have to manually filter through all the cruft.



Share this post


Link to post
Share on other sites
There''s a search that people shold use that can answer a lot of their questions. But they don''t. Why would they look through the stickys? Also the stickys would take up the first 30 pages or so and you would have to wade through them all to get to the newest posts each time.

Share this post


Link to post
Share on other sites
quote:
Original post by iMalc
If you''re constantly switching between C and C++ you might find it easier to just use structs.
If they read it the first time (and thus were aware there was an original post), they probably wouldn''t post the same thing.

I don''t know about the rest of you, but I don''t spend the entirety of my free time reading every post here.

Share this post


Link to post
Share on other sites
C++ structs can have member functions.

Share this post


Link to post
Share on other sites
quote:
Original post by petewood
I have found that forward declarations in MSVC++ have to match types so if something is a struct and you forward declare it as a class it won''t allow it.


This is because they are considered different types (the closest analogy I can think of, is two classes with the same name in different namespaces, except they''re not in different namespaces so it''s a collision and fails to compile).


quote:
zfod
With all these moderators on the board, whom I actually respect most of their opinions ( woefully rare in the forum world ), why are these kinds of posts made all the time? Why aren''t there literally stickies with all the frequently asked questions and automatic deletion of threads that are answered in said stickies?


Then we''d have two or three pages of stickies and people still wouldn''t read them

Share this post


Link to post
Share on other sites
quote:
Original post by zfod
The question that sticks out in my mind is this:

With all these moderators on the board, whom I actually respect most of their opinions ( woefully rare in the forum world ), why are these kinds of posts made all the time? Why aren''t there literally stickies with all the frequently asked questions and automatic deletion of threads that are answered in said stickies?
a.) It''s tedious.
b.) Nobody reads the FAQs we labor so hard over - not even the site FAQ when they register!
c.) Netiquette is dead. The days when you could expect people to know to search before asking, and to attempt before requesting assistance/a solution, are gone. Forever.

quote:
It seems it would clean the board up tremendously in a lot of the sections, make the board smoother, take up less space and possibly make searches return better results, etc.
What you are suggesting is an evolving set of FAQs. Wait, isn''t that called a Wiki...? Apparently, community support for a Wiki is low. So, this is what you get instead.

Besides, it''s not the same people who respond. What tends to happen is that a person joins with a certain low level of overall knowledge. This person starts out asking questions and later moves to answering them. Over time this person gets to answer or read answers to all the recurring questions, finally gets bored and moves on to other things (how often do you see posts from DrPizza? SabreMan? Even Fruny seems to respond most in For Beginners - you can''t fault him; he''s a TA). Stagger this pattern infinitely, with one "generation" "graduating" as another joins up. It''s only the die-hards (and moderators) who really get frustrated because they kinda have to stick around.

quote:
Anyway, sorry for highjacking the thread.. it''s just been on my mind a lot when I have to manually filter through all the cruft.
Well, Dave has expressed interest in the Wiki. We''d have to discuss it again (even the mods weren''t too enthusiastic), modify the concept heavily so that it would be useful to GDNet, test it, populate it and then launch it.

Don''t hold your breath.

Share this post


Link to post
Share on other sites

  • Advertisement