Smart Pointers, Forward Declaration and C4150

So, as part of a large hobby training project, I implemented basically the full implementation of smart pointers. He does almost everything that I ask about this, with the exception of one small detail that may turn out to be a violator of the transaction if I cannot solve it. The above example:

//Header1.h

#include <Header2.h>

class A
{
//Methods and such that involve class B in return type / arguments
};

//Header2.h

class A; //Forward declaration of A, needed because A includes Header2.h

class B
{
public:
    SmartPointer<A> Ptr;
};

The previous code, as you might have guessed, gives me warning C4150: deletion of pointer to incomplete type 'type'; no destructor called. I know why this is happening; c Header2.h, the smart pointer code includes deletion in the forward-declared instance A. If I could turn on Header1.h, no problem. At the moment, I really do not want to refactor.

, boost - . , / . , ? ? , , , SO .

, , .

+3
3

, ?

Boost , checked_delete delete , A.

+2

Boost , . . , - , delete.

shared_ptr - , , . , , . .

+1

, ( ) CPP B, :

//Header1.h
class A
{
//Methods and such that involve class B in return type / arguments
};


//Header2.h

class A; //Forward declaration of A

class B
{
public:
    ~B();

    SmartPointer<A> Ptr;
};


//Header2.cpp
#include "Header2.h"
#include "Header1.h" // obtain full definiton of A

B::~B() = default; // destructor here knows full definition of A

, , , SmartPointer , B, ~ B, . , Header2.h, , . Header2.cpp Header2.h, , , Header2.cpp, . , Header1.cpp, , ~ B . ~ B , .

In certain circumstances, you may need to go to cpp, as well as to the assignment operator (s) B and / or, for some inexplicable reason, even the constructor.

0
source

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


All Articles