Forward declaration / when is it better to include headings?

It is very clear to me when I can / cannot use the forward declaration, but I'm still not sure about one thing.

Let's say I know that sooner or later I have to include a header to remove a reference to an object of class A. I don’t understand if it’s more efficient to do something ...

class A;
class B
{
   A* a;
   void DoSomethingWithA();
};

and then in cpp there is something like ..

#include "A.hpp"
void B::DoSomethingWithA()
{
   a->FunctionOfA();
}

Or could I just include the header header in header file B first? If the former were more efficient, I would appreciate it if someone had clearly explained why, since I suspect that this has something to do with the compilation process, which I could always find out by learning more.

+5
source share
3 answers

( ), . , , , , . , , , .

Google :

+11

, " B A, , A ". , .. , , .

, , , .

:
?
?

+4

. . A.hpp B.hpp.

The standard practice for C and C ++ header files is to wrap the entire header file in #ifdef to ensure that it is compiled only once:

#ifdef _A_HPP_
#define _A_HPP_

// all your definitions

#endif

Thus, if you are #include "A.hpp"in B.hpp, you may have a program that includes both, and it will not break, because it will not try to determine something twice.

0
source

All Articles