Jens Bodal
Jens Bodal

Reputation: 1757

Should a header file include other header files, or leave it to the implementation?

So I can get everything to compile, and I understand (I think) how include guards prevent the same definitions from being pulled twice, but what I don't know is if my header file should also include the header file for the class it uses, if my class file already includes it.

Child.hpp

// Child.hpp
#ifndef CHILD_H
#define CHILD_H

class Child {
    public:
        Child();
};

#endif

Child.cpp

// Child.cpp
#include "Child.hpp"

Child::Child() {
    // example
}

Parent.hpp

Should I also include Child.hpp here even though it's already included in Parent.cpp? I understand the header guard would prevent Child being defined twice, but is it considered best practice? Or should I exclusively include Child.hpp here?

// Parent.hpp
#ifndef PARENT_H
#define PARENT_H

class Parent {
    public:
        Parent();
        Parent(Child child);
};

#endif

Parent.cpp

// Parent.cpp
#include "Child.hpp"
#include "Parent.hpp"

int main() {
    Parent parent;
    return 0;
}

Parent::Parent() {
    // example
}

Parent::Parent(Child child) {
    // example
}

We were just given one example in class, and it essentially said that in Parent.cpp it should include Parent.hpp (makes sense) and Child.hpp.

It would seem I would want to include Child.hpp in Parent.hpp as the Parent class relies on the Child class, but either way Child.hpp gets included.

Upvotes: 3

Views: 3538

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

If Parent has any instances of Child, yes you must include the header to Child.hpp.

class Parent {
    public:
        Parent();
        Parent(Child child);     // Need full include, complete type used
        Child c;                 // Need full include, complete type used
};

If Parent only had pointers or references to Child you could get away with simply forward-declaring Child, then doing the include in Parent.cpp.

class Child;                     // Forward declaration
class Parent {
    public:
        Parent();
        Parent(Child* child);    
        Child* pc;               // Incomplete type ok if pointer or reference
};

In general, you should avoid including headers inside other headers unless absolutely necessary. At best, it increases build times unnecessarily. At worst, it can lead to circular dependencies.

Upvotes: 4

Related Questions