Jump to content
  • Advertisement
Sign in to follow this  
Axiverse

C++ Structs vs Classes

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

This has just recentally been brought up between a friend, but what is the differences between structs and classes in C++ From what I know the only difference is that structs don't get their memories cleared upon creation and classes do. They both support templates and inheritance. Any other discrepencies?

Share this post


Link to post
Share on other sites
Advertisement
The only differences between structs and classes in C++ is that the default access and inheritance for structs is public and the default access and inheritance for classes is private.

Neither get their "memory cleared on creation", but both may have a constructor called.

Share this post


Link to post
Share on other sites
I understand access, but what do you mean inheritance being public/private and then the memory cleared doesn't really exist (except as a figment of my imagination)?

Also can a struct inherit a class or vice versa?

Share this post


Link to post
Share on other sites
By default inhieritance I mean that these two types are essentially the same:

struct A : B, private C {
};
class A: public B, C {
};

And neither structs nor classes automatically get their memory cleared unless thats what their constructor is defined as.

Share this post


Link to post
Share on other sites
Yeah i understand that, but what I ment is what public /private inheritance affects other code. Will you not be able to typecast to private parent class types?

Share this post


Link to post
Share on other sites
Public inheritance is probably what you consider to be normal inheritance. With public inheritance the derived class has an ISA relationship with the base class. With private inheritance the derived class inherits the implementation of the base class, but does not appear to be of base class type to outside code. Also, the member functions and variables of the base class are treated as private in the derived class.

Share this post


Link to post
Share on other sites
I'm not sure what exactly you are trying to say ... something like this ...

struct IObject
{
virtual void update() = 0;
};

class CObject : public IObject
{
public:
void update();
};

would be the same as ...

class IObject
{
public:
virtual void update() = 0;
};

struct Object : public IObject
{
void update();
};

Share this post


Link to post
Share on other sites
The most important technical difference is that you can use structures in headers export portable C interfaces. Another difference is that the class keyword can be used to declare template types, though the newer typename keyword is a lot more logical.
There's probably one or two obscure ones that we've missed though..

The rest is a matter of convention but at least as important anyway.

Share this post


Link to post
Share on other sites
class MyClass : public IObject
{
public:
int stuff;
};

is that same as

struct MyClass : IObject
{
int stuff;
};

To make a struct behave like class, substitute public with private.

(This ought to be in the FAQ)

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
The only differences between structs and classes in C++ is that the default access and inheritance for structs is public and the default access and inheritance for classes is private.

Neither get their "memory cleared on creation", but both may have a constructor called.


^^^ What he said. I had a similar discussion with a friend a few years ago that cleared this up for me. :)

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!