c++ - How many other files does include affect aside from the one it's in? -


i'm learning c++, , under presumption including affect file included it, appear wrong. setup so:

enter image description here

if don't include baseclass.h in either main or subclass (extends baseclass), seemingly-normal error telling me can't use baseclass in subclass because doesn't exist. if include baseclass.h in subclass.h, works fine. if don't include baseclass.h in subclass.h, include in main cpp, works fine? @ first thought meant including in 1 file included whole project, i'm not sure because baseclass.cpp included baseclass.h whole time , got error when wasn't included in main/subclass.

why able interchange inclusion of baseclass.h between main cpp , subclass.h while having function in both cases? how many files affected when include something?

i'm working in eclipse neon mingw gcc project, i'm not sure if affects behavior. here source code i'm working with:

the main cpp, reproproject.cpp:

#include <iostream> #include "baseclass.h" #include "subclass.h"  using namespace std;  int main() {     subclass s;     cout << "hello world!" << endl; // prints !!!hello world!!!     return 0; } 

baseclass.h

#pragma once  class baseclass { public:     baseclass(); }; 

baseclass.cpp

#include "baseclass.h"  baseclass::baseclass() {  } 

subclass.h

#pragma once  class subclass: public baseclass { public:     subclass() : baseclass() {} }; 

the effect of #include if referenced file's contents replace #include statement itself.

your subclass class derived baseclass. subclass.h header file declares subclass, therefore when compiling subclass.h, definition of baseclass must available.

except, , key point, not compile subclass.h. compiling retroproject.cpp. compiler compiling. or, rather, begins compiling.

your compiler starts compiling retroproject.cpp, reading file beginning end. each time encounters #include, referenced file's contents replace #include statement, , compiler resumes reading , compiling code, starting #included text. when sees #include, process repeats.

so, if have #include "baseclass.h" in subclass.h, following happens. when retroproject.cpp #includes subclass.h, contents of subclass.h replace #include statement in retroproject.cpp, then, compiler resumes compiling, sees #include, , replaces #include contents of baseclass.h.

the compiler parses baseclass.h, defines baseclass, , compiles rest of subclass.h, defines subclass, , since baseclass defined, there no issue.

if explicitly #include baseclass.h file in retroproject.cpp, compiler first replaces #include contents of baseclass.h, compiles it, defines baseclass, #include of subclass.h file gets processed same away, and, once again, should defined, defined.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -