class and struct
Is there any difference between a class and a struct other that the private/public init?
Why does everyone use classes for "big" things and structs for small?
Thanks.
well back in the days of C, programmers used structs to groups various variables together. in C, structs can only have PODs, struct, and pointers of PODs and structs. now when C++ came along alot of the C programmers stuck with that same philosophy. they would use classes (you know variables with functions and stuff) for the objects and structs for moving small sets of data.
now. even though in C++ structs and classes are equivalent their uses are a throwback from the C days.
vets, please feel free to correct or flame anything i just said.
now. even though in C++ structs and classes are equivalent their uses are a throwback from the C days.
vets, please feel free to correct or flame anything i just said.
Quote:OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.
Wow did that dude read my mind ? [lol]
there is one other minor difference. if you forward declare a struct as a class, you'll get a redefinition error.
Quote:Original post by ChaosEngine
there is one other minor difference. if you forward declare a struct as a class, you'll get a redefinition error.
I don't...
class X;void foo( X * );struct X{public: int y;};void foo( X * x ){ x->y = 10;}int main( int argc, char **argv ){ X x; foo( &x); // ...}
IIRC Bjarne said that writing this:
In C++ is ONLY a short-hand for doing this:
Only IIRC, though.
struct {
In C++ is ONLY a short-hand for doing this:
class { public:
Only IIRC, though.
Quote:Original post by Red Ant
A decent compiler should at least give a warning, though.
Why? Given that while using a forward declaration you cannot use any members whatsoever, whether the thing is really struct or a class is irrelevant. By the time you have gotten to code that can use the struct/class, the relevant details would be known...
Quote:Original post by rip-offQuote:Original post by ChaosEngine
there is one other minor difference. if you forward declare a struct as a class, you'll get a redefinition error.
I don't...
*** Source Snippet Removed ***
sorry you're completely right. It does issue a warning, but I don't know why I thought I got an error before...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement