Jump to content
  • Advertisement
Sign in to follow this  
MTclip

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

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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..?

Share this post


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

struct A
{
//stuff
};


Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!