Ozan
Ozan

Reputation: 13

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.

Upvotes: 1

Views: 553

Answers (2)

alk
alk

Reputation: 70911

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.

Upvotes: 1

geza
geza

Reputation: 29952

There's no such thing as indirect including in the way you describe it. If you include foo.h into doo.cpp, then foo.h will be available in doo.cpp only, it won't be available in files which include foo.h. You need to include foo.h into doo.h as well, to make contents of foo.h available for users of doo.h.

#include is just simple text processing, nothing more.

Upvotes: 1

Related Questions