🔒 Locked
Structs vs Classes in C++
Advertisement
Original Post
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]
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:
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).
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).
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.
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.
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.
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.
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.
why do people repeate what others have already posted...lol
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.
class Token;
struct Foo;
struct Bar;
class Generaliser;
class Opener;
class CanOpener;
class BottleOpener;
struct pointerDeleter;
etc.
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.
quote:If they read it the first time (and thus were aware there was an original post), they probably wouldn''t post the same thing.
Original post by iMalc
If you''re constantly switching between C and C++ you might find it easier to just use structs.
I don''t know about the rest of you, but I don''t spend the entirety of my free time reading every post here.
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
quote:a.) It''s tedious.
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?
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: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.
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.
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: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.
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.
Don''t hold your breath.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement