c++.. struct or class....?

Started by
8 comments, last by Shannon Barber 18 years, 8 months ago
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..?
Advertisement
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};
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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 Promit
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 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 SiCrane
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.


*shrug* I always felt that was a logical extension of the scope, myself. And yeah, I meant private. Gah.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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.
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.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
I use struct if the (majority of) data members are to be public. Otherwise, class.
I use struct for everything.
A class without any public members is exceedingly rare.

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement