Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Boops

Structs vs Classes in C++

This topic is 5277 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

I know two differences between structs and classes: -structs date from C but were improved in C++ -classes are new, and while in structs everything is public by default, in classes it isn't. Are there any other differences than these? Is one of them faster than the other? Is there a reason to use one or the other, are both useful for something or is it better to always use classes? Also, another question (kinda newbie): what's the difference between the "." and "->" symbol in C++? [edited by - Boops on March 5, 2004 7:21:41 PM]

Share this post


Link to post
Share on other sites
Advertisement
One more difference off the top of my head: inheritance with structs is public by default, but private by default with classes.

. is used with the actual objects or references.

-> is used with pointers.

Share this post


Link to post
Share on other sites
structs and classes are EXACTLY the same, except that the default access type is public in structs, and private in classes. So the follwing two are exactly the same:
class Foo {public: int i};
struct Foo {int i};

Theres no other differences - inheritance works the same, member functions work the same, etc etc.

However, since struct is from C, people tend to use a struct to represent a general data type without member functions (or maybe a constructor / destructor).

Share this post


Link to post
Share on other sites
The one and ONLY difference between structs and classes:

Members of structs are public by default.
Members of classes are private be default.

Although many people choose to impose arbitrary restrictions on them; for example, I never add functions to structs, only classes. Ctor/dtor maybe, but no member functions.

Share this post


Link to post
Share on other sites
quote:
Original post by Evil Steve
Theres no other differences - inheritance works the same, member functions work the same, etc etc.


Inheritance does not work the same. I''ll say it again: Inheritance for structs is public by default. Inheritance for classes is private by default.

Share this post


Link to post
Share on other sites
Yup, so pretty much the only reason to pick one over the other is style.

Coders generally use structs for things that are inert, they store data but they don''t do anything, all their members are public, they don''t have functions (or maybe they have a couple accessor functions), and they can be safely shallow copied.

Classes are generally used for objects that actually do something, they have private data, and they have functions that perform actions.

Whether or not you want to follow that distinction is completely up to you, but that''s the way it''s generally done.

Share this post


Link to post
Share on other sites
If you''re constantly switching between C and C++ you might find it easier to just use structs.

Share this post


Link to post
Share on other sites
I have found that forward declarations in MSVC++ have to match types so if something is a struct and you forward declare it as a class it won''t allow it. To be consistent and not have to look things up to see whether they are a class or a struct I make everything classes. I know the compiler catches it but you''ll end up with a list of forward declarations like this.

class Token;
struct Foo;
struct Bar;
class Generaliser;
class Opener;
class CanOpener;
class BottleOpener;
struct pointerDeleter;

etc.

Share this post


Link to post
Share on other sites
quote:
Original post by commander keen
why do people repeate what others have already posted...lol


This isn''t the question that sticks out in my mind. The question that sticks out in my mind is this:

With all these moderators on the board, whom I actually respect most of their opinions ( woefully rare in the forum world ), why are these kinds of posts made all the time? Why aren''t there literally stickies with all the frequently asked questions and automatic deletion of threads that are answered in said stickies?

It seems it would clean the board up tremendously in a lot of the sections, make the board smoother, take up less space and possibly make searches return better results, etc.

Anyway, sorry for highjacking the thread.. it''s just been on my mind a lot when I have to manually filter through all the cruft.



Share this post


Link to post
Share on other sites

  • 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!