Difference between include and forward directive in C ++

I have to reorganize the old code. One of his problems is that it exceeds the useless "includes." In the same project, I saw the following syntax:

#include <AnyClass> //A system header #include "AnotherAnyClass" //A application header class AnotherClass; class Class : public OneMoreClass { public: explicit Class(); ~Class(); private: AnotherClass *m_anotherClass; } 

I would like to find out:

  • What are the differences between include class and class class?
  • When should the second method be used and how?
+6
source share
3 answers

These are two different things:

 #include <AnyClass> 

this is a normal include for the header (or any type of text) of the file. This is equivalent to pasting the contents of the AnyClass file to the place where you typed this inclusion directive (which includes strains and / or compilers, which are often used to prevent multiple inclusions in a single file).

This syntax is:

 class AnotherClass; 

represents forward declaration and informs the compiler about the existence of the AnotherClass class. It is useful in many scenarios, for example, suppose you have two classes, each of which needs a pointer to the other:

 class ClassB { ClassA* pointer_to_classA; }; class ClassA { ClassB* pointer_to_classB; }; 

the above code generates an error: "error: unknown name of type" ClassA ", because you used the ClassA type, not the compiler, knowing that it is (for now). The compiler parser only recognizes the existence of ClassA when it analyzes the start of the class declaration.

To complete the above work, you will need the following declaration:

 class ClassA; // A ClassA type exists class ClassB { ClassA* pointer_to_classA; }; class ClassA { ClassB* pointer_to_classB; }; 
+5
source

The class declaration is as follows:

 class AnotherClass; 

called Forward Declaration . This allows the compiler to learn about the existence of a class in situations where unknown details about the class are not needed, for example, in a script you published.

As a rule, if you are not going to use an instance of a class, you simply deal with a pointer and do not call any methods on it, you do not need to know anything more than the class name. This can speed up compilation in large projects.

+6
source

To understand the difference, you must understand why the compiler needs to be turned on first.

The compiler must know the size of each class you declare. Thus, he must know the size of each member added to the class. In the case of a non-pointer non-inline member, the compiler should see the definition of this member class, which is usually in the header that you should include. In the case of a pointer, however, the compiler does not need to see the whole definition, because, regardless of its type, the pointer always has the same size on this platform (usually 32 or 64 bits). Therefore, when an element is a pointer, the compiler needs to know only the name, which can be executed by direct declaration, i.e.

 class AnotherClass; 

Now which one to use?

Shortcut: Use forward ad whenever you can, enable when you need to.

If your headline can do a great job ad forward, you will reduce the total compilation time because every heading you include must be handled in every translation unit that contains it.

The general scheme is to forward the declaration in the header and include the corresponding class in the associated .cpp .

+4
source

Source: https://habr.com/ru/post/977131/


All Articles