Search code examples
c++includec-preprocessorprototype

Is that possible to include a header file in an indirect way in C++


First of all, I am a very beginner in programming. So that before answering, please consider that...

Let's say I have 3 different source code files. First one is foo.cpp, second is doo.cpp, and third loo.cpp. In the doo.cpp, I am including the header file of foo.h:

#include "foo.h"

Doo::Doo(){
//  something
}

and in the other file loo.cpp, I am including the header file of doo.h:

#include "doo.h"

Loo::Loo(){
//  something
}

My question is; to use in loo.cpp, will I have access to create an object from the Foo class which is declared in foo.h, without including it in loo.cpp, but having included doo.h which has the inclusion of foo.h. Can I do something like this? :

#include "doo.h"

Loo::Loo(){
Foo object = new Foo();
}

So like at above, can I reach foo.h without including it in the loo.cpp but having another source file which has been included foo.h in its own source file ? It is simply like an indirect including if it's possible.

Note: I don't know if I explained my question so terrible, so please feed me back so that I can edit my question to make more understandable.


Solution

  • to use in loo.cpp, will I have access to create an object from the Foo class which is declared in foo.h [...] having included doo.h which has the inclusion of foo.h.

    Yes.

    If

    • loo.cpp included doo.h and
    • doo.h includes foo.h

    then foo.h is included in loo.cppas well.

    You need prototypes. If they were included into any translation unit (.cpp) you could check by running the pre-processor on it: cpp foo.cpp and inspect its output, which in fact is the code fed to the compiler.