Sign in to follow this  
daniel_i_l

class and struct

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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);
// ...
}

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this