Ryan Sayles
Ryan Sayles

Reputation: 3431

C++ header inclusions

Hello I'm new to C++ so bear with me, I'm making a header for a class and I just want to know if I plan on making an object from another class in this one should I include that in the header? for example

class myClass1{

public:
    "constructor and methods here"
private:
    OtherClass oc;
    "other variables here"
};

I know this is a pretty simple question but I can't seem to find the answer anywhere. Any help would be appreciated!

Upvotes: 0

Views: 135

Answers (3)

John Dibling
John Dibling

Reputation: 101456

Since as you say that you are new at C++, this might seem a bit overkill for your immediate needs. Keep in mind what I'm trying to teach you is not how to hack this thing up right now, but good coding skills that will transfer to future projects and real work.

A stretch goal, and a general rule of thumb that I would recommend is:

In a header file, #include nothing, or as close to nothing as is possible.

Why? There are at least two reasons.

First, it keeps compiles fast. This is nice, but not the real reason. The real reason is:

Second, it reduces interdependence between modules and hard couplings. An interdependence between modules is an easy thing to create, and a very difficult thing to break when it becomes a problem.

Contrary to the other answers posted here, no, you do not need to #include OtherClass's header file in this header file. This assertion yields two questions:

  1. How is it possible to not include the header?
  2. Why would you not include the header, even if it is possible not to?

In order:

The header file doesn't need to know anything about the definition of OtherClass. Only the translation unit does. The TU needs to know the definition of OtherClass before it can define MyClass1, but that is handled simply. Consider:

OtherClass.h:

#ifndef OTHERCLASS_H
#define OTHERCLASS_H

class OtherClass
{
};

#endif

MyClass.h:

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass
{
public:
  OtherClass mOC;
};

#endif

main.cpp:

#include <cstdlib>
#include "OtherClass.h"
#include "MyClass.h"

int main()
{
  MyClass mc;
}

The Translation Unit here is main.cpp and everything it #includes. The whole package. Since you do not compile header files directly (eg by themselves), headers are not translation units.

I suggest that what I propose above is better than adding:

#include "OtherClass.h"

to MyClass.h because it reduces the interdependence between the two objects.

This obviously isn't a concern right now, or in a toy program such as this. It becomes an issue later, in large & complicated codebases when you try to make changes to underlying classes. At those times, breaking these interdependancies becomes exceedingly difficult and maybe impossible depending on how much forethought you have given your design. This becomes a lesson learned with great difficulty.

Upvotes: 1

zero298
zero298

Reputation: 26877

Yes include using #include "OtherClass.h". If you were using a system header or maybe something from STL, you would use: #include <SomeSystemHeader>

Upvotes: 0

Jason
Jason

Reputation: 499

Yes. If oc was a pointer or a reference, then you could get by with just a forward declaration (which appears above the class statement):

class OtherClass;

class myClass1 {
...

The include statement is needed because the compiler needs to know the details of OtherClass to define myClass1.

Upvotes: 0

Related Questions