Advanced declarations and includes

When working with a library, be it my own or external, there are many classes with forward declarations. The same classes are also included depending on the situation. When I use a specific class, I need to know if certain objects that the class uses are declared, or if they include #include (d). The reason is because I want to know if I should include both headers or only one.

Now I know that this is pretty simple to check, just compile (or look through all the headers until you find the object in question, either #include (d) or declared ahead). But with large projects, a simple compiler takes a lot of time, and each include file checks to see if the class in question is included or if a delay is announced, but this is tedious, especially if it's several levels. To illustrate the problem further:

Class A is class B, it is class C that has a pointer to class Foo

I turn on class A and want to know if the Foo class is ready to be used right away, or I need to do the following:

#include "ClassA.h"
#include "ClassFoo.h"

So, is there a quick and easy way to find out about this dependency without compiling or going into dependent headers? (one of the reasons why I ask this question is because I want, in the end, to make an add-on for VS and / or a stand-alone program for this, if the functionality does not exist yet)

Thank!

+3
source share
3 answers

In the source file, you should not depend on what other header files are included.

You must explicitly include all the types your code depends on.
If you are not using a type (i.e., simply passing a pointer or a link around), you do not need to define it, so do not include its header file (the header file for your methods should have at least a forward declaration).

. Foo, "ClassFoo.h", . ( ). , , ( ) .

: . / .

+7

, , -, hella more. . . #include -, , (#includes - ), , ). Foo Bar, Foo.h "Bar.h", forward. Foo.cpp Bar.h.

foo.h

class Bar ; // fwd declare wherever possible

class Foo
{
  // I can haz needs Bar
  void do( Bar *bar ) ;
} ;

foo.cpp

#include "Foo.h"
#include "Bar.h" // I can has needs complete implementation details.

Foo::do( Bar *bar )
{
}

bar.h

class Bar
{
  // code..
} ;
+1

class A, C (, , A). , access Foo , C . Foo, C. Foo C - , , C.h Foo.h, , .

0

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


All Articles