c++.. struct or class....?
ok i presume this may be a matter of preference none the less i will ask..
why would some one use a struct that contains constructors destructors and functions .... would it not be more sensible to implement it as a class..
are there differences..?
Quote:Original post by MTclip
are there differences..?
There is One (1) and exactly One (1) difference.
The default scope of a struct member is public. The default scope of a class member is private.
Put another way...
class A{public://stuff};//is EXACTLY the same asstruct A{//stuff};
Quote:Original post by Promit
There is One (1) and exactly One (1) difference.
The default scope of a struct member is public. The default scope of a class member is protected.
And default inheritance for a struct is public and the default inheritance for a class is private.
It's mostly a matter of preference. People generally use classes for things that have behavior, and structs for things that just hold data. But it's not a strict rule.
Quote:Original post by PromitQuote:Original post by MTclip
are there differences..?
There is One (1) and exactly One (1) difference.
The default scope of a struct member is public. The default scope of a class member is protected.
Put another way...
*** Source Snippet Removed ***
private, not protected. Also, inheritance defaults to private as opposed to public as Sicrane clarified.
Quote:Original post by SiCraneQuote:Original post by Promit
There is One (1) and exactly One (1) difference.
The default scope of a struct member is public. The default scope of a class member is protected.
And default inheritance for a struct is public and the default inheritance for a class is private.
*shrug* I always felt that was a logical extension of the scope, myself. And yeah, I meant private. Gah.
It comes down to preference, though my preference is to type less. I look at it this way:
Every (ok there might be an exception somewhere) types will need public members. Thus if I use the "class" keyword to define my types I will always have to type "public:" somewhere.
Some types will need private members. Thus if I use the "struct" keyword to define my types I will sometimes have to type "private" somewhere.
Obviously I would rather do less typing so I switched over to using "struct". I came from a java background so I had to conciously change my habits to start using "struct", as I had never used it before, I did not even learn the basics of C or C++ until I had been programming in java for two years. My muscle memory always tried to type "class" but now that habit is broken and I always use struct.
Every (ok there might be an exception somewhere) types will need public members. Thus if I use the "class" keyword to define my types I will always have to type "public:" somewhere.
Some types will need private members. Thus if I use the "struct" keyword to define my types I will sometimes have to type "private" somewhere.
Obviously I would rather do less typing so I switched over to using "struct". I came from a java background so I had to conciously change my habits to start using "struct", as I had never used it before, I did not even learn the basics of C or C++ until I had been programming in java for two years. My muscle memory always tried to type "class" but now that habit is broken and I always use struct.
What I (and lots of other people) have a tendency to do is to write structs for POD types, and classes for everything else. Of course, you inevitably end up writing a constructor...and frequently a destructor...maybe a copy ctor and a few other ctors...a couple quick functions to do things...
The lines get blurry pretty fast.
The lines get blurry pretty fast.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement