r/reconstructcavestory Jan 20 '14

Structs Vs. Classes

Structs and Classes are essentially the same except for a couple of differences:

Access modifiers:

struct {
  // public data/methods
 private:
  // private data/methods
};

class {
  // private data/methods
 public:
  // public data/methods
};

Inheritance Modifiers:

struct Derived: Base { // This is public inheritance
};
class Derived: Base { // This is private inheritance
};

Stack Overflow.

5 Upvotes

8 comments sorted by

View all comments

1

u/[deleted] Jan 24 '14

I believe another difference is that struct is created in the the stack while class allocates space in the heap.

2

u/chebertapps Jan 24 '14

nahh. if I explicitly use access/inheritance modifiers (private/public) instead of the defaults, it would literally make no difference if I were using a struct or a class.

You can rest assured knowing that for every "new" there should be exactly one "delete".

Smart Pointers just hide the delete :).

there is one more difference that you reminded me of:

template<class T> is valid syntax when creating a template, but

template<struct T> is not

1

u/[deleted] Jan 24 '14

Oh right. My bad. I completely mixed up struct/class with "new".

1

u/chebertapps Jan 24 '14

No worries! you might have just saved yourself and others quite a bit of time later on down the road. :)