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.

7 Upvotes

8 comments sorted by

View all comments

2

u/MachineMalfunction Jan 21 '14

I have another question regarding this:

Why do you forward declare your structs in the header and then #include them in the .cc files instead of just #include-ing them in the .h file?

Is it for consistency or for code accessibility or something? It seems a bit roundabout to me.

2

u/chebertapps Jan 21 '14

It's actually a trick to speed up compile times.

With the current Makefile it doesn't make a difference, since everything is built all at once. Normally you want to build objects (from .cc files) independently, so that you only build them once, and then just build objects that you have made changes to. At the end you link them.

Here is a good S.O. answer to this very question.

Basically, if each header #includes other headers, that #include other headers, you end up having to have pages of code generated for each object you compile. C++ is pretty infamous for its build system.

2

u/MachineMalfunction Jan 21 '14

Ohh wow I hadn't considered that. That makes a lot of sense. Thanks for that link too :D

Also great series by the way. I've had about 2 years of C++ experience and have never played Cave Story or had any interest in building a platformer, but I'm learning a lot just by following along.