class and struct

Started by
9 comments, last by jpetrie 17 years, 10 months ago
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.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
Septagonal
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.

Beginner in Game Development?  Read here. And read here.

 

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.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
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);    // ...}
A decent compiler should at least give a warning, though.
IIRC Bjarne said that writing this:
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-off
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...

*** 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...
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement