What are the disadvantages of a forward declaration?

I am wondering if there are drawbacks to using forward ads in all places whenever possible. This is if my headline contains only ads.

As far as I understand, using forward declarations speeds up compilation time, but I don’t know any disadvantages as such.

Example:

hijras:

Class A
{
};

bh:

// Should I use and include "a.h" in the cpp file (e.g., a.cpp)
Class A;
Class B
{
    doSomething(A *a);
    A *myA;
};

Or is it better to use

bh:

#include "a.h"

Class B
{
    doSomething(A *a);
    A *myA;
};
+3
source share
5 answers

. "A.h" , . , ( , ), , , B - class A, , *.

( ) , , - , : , , :

class B
{
    A myA[10];
};

, A. .

* B A. B, ; , , B.

+10

, ( ) , .

, .

- .

+2

. :

  • . , , , A B .

  • . a.h , b.h. . , Pimpl.

:

  • , , , , ( A , B.h). .

  • , , , , , . , , B A . , / , B A ( pimpl). , , , , , -, . , , , , / .

+1

- .

, , . , . , , , .

+1

The only drawback that comes to mind is that forward ads require pointers. Therefore, they cannot be initialized and, therefore, can lead to the exclusion of a null reference. Since the coding standard that I am currently using requires all pointers to need to check for a null value if code highlighting can be added. I started around this with Design By Contract ; then I can argue that all initialized in the constructor are never null.

0
source

All Articles