Jump to content
  • Advertisement

Archived

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

amish1234

c++ structs

This topic is 5207 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 always thought that structs were like a class that had no methods and couldn''t be inherited. I believe this is true in C. However, C++, having classes, seems to have made structs much more similar to a class. Right now I''m looking at code that has structs with methods and constructors/destructors and struct inheritance. So what''s the difference between structs and classes in C++? ___________________________________________________________ Where to find the intensity (Updated Mar 20, 2004)

Share this post


Link to post
Share on other sites
Advertisement
structs'' members are public by default, whereas classes'' are private by default. both can be overridden by using the "private:" or "public:" tags.

structs default to public inheritance, classes default to private inheritance.

the following are exactly the same:

struct Base
{
int publicVar;
private:
int privateVar;
};

struct Derived1 : Base
{
};

struct Derived2 : private Base
{
};



class Base
{
int privateVar;
public:
int publicVar;
};

class Derived1 : public Base
{
};

class Derived2 : Base
{
};

Share this post


Link to post
Share on other sites
Did you even consider searching the forums before you asked this? Not only that, you''ve actually participated in these struct vs. class threads before; like here. What''s even worse is that in *that* thread you remembered that structs could have constructors.

Share this post


Link to post
Share on other sites
Well, when I wrote that reply, I''d seen structs with constructors before, but not other methods, and I hadn''t seen inheritance with structs.

___________________________________________________________
Where to find the intensity (Updated Mar 20, 2004)

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!