c++ - same #include statements in derived classes -
I have two questions
We say that there are a base class and many derivative classes, derived Classes are all going to use the #include
statement (as #include
etc.) and base class of
lines . 1. It is considered a good practice to write lines in derivative classes using the #include
statement of the base class and using lines. File anyway? 2. Question about structure - The purpose of class B in class B in class A is, what is the purpose of class B,
s
# include` statements in a good practice File anyway?
Thank you!
say that there is a base class and many derivative classes, all #include in derivative classes The statement (such as
#include
etc.) and is going to use lines of base squares.gt;
Not at all.
First of all, you place not on top-level scopes in the lines by using Second, do you think that the derived class needs all the . This is a common starting mistake.
s base class? The derived class header file is required to include a base-class header file, and the derived class implementation file needs to include a class header file generated.
It already includes all of you.
base.h:
#include & lt; String & gt; Class base {// ... virtual std :: string f (); // some `experiment`};
derived.h:
#include "base.h" // < String & gt; Category Derived Here: Public Base {// ... virtual std :: string f (); };
derived.cpp:
#include "derived.h" // any & lt; String & gt; Here std :: string Derived :: f () // can be used
Now absolutely, it It is technically possible to do this in a more complicated manner, such as:
base.h:
// not Gt; Class base {// ... virtual std :: string f (); };
derived.h:
#include "base.h" // still not & lt; String & gt;? Class derivative: public base {// ... virtual std :: string f (); };
derived.cpp:
#include & lt; String & gt; // ah, ok # include "derived.h" std :: string Derived :: f () {// ...}
This works only because the compiler is separately
But compile the hemder files while talking about the awesome programming style, but only the compilation units (~ ..cpp files) are completely included. Why do you want to include extra headers to force everyone to get it from their class?
2. Question about structure - The purpose of class B in class A in class A is, is it a good practice to write BS in the HK # included statement? File?
It depends if the heading of A
needs access to any B
members, then you include one , And in that case, again, whatever you are required if the heading of A
should be placed in the context of only one indicator or B , then < B>
/ code> or just a return value, you can use a Forward Announcement to speed up the compilation. Of course, to accelerate the compile, nothing should be done about anything that starts C ++, because it will go through a lot of time until you will be able to develop the software that takes several hours to build):
However, for perfection:
ah:
class B; // Further declaration, not #include class A {// ... bf (); B * PTR; B & amp; Referee; };
a.cpp:
#include "ah" #include "bh" // contains: BA :: F () {b OBG ; Return obj; }
b.h:
square B {// ...};
Comments
Post a Comment