q****x 发帖数: 7404 | 1 The code is from stack overflow. why the dtor must be defined in .cpp,
instead of .h? Moving it to .h will lead to a compilation error.
// A.hpp
#include
class B;
class A {
std::unique_ptr myptr;
// B::~B() can't be seen from here
public:
~A();
};
// A.cpp
#include "B.hpp"
// B.hpp has to be included, otherwise it doesn't work.
A::~A() = default; // without this line, it won't compile
// however, any destructor definiton will do. | p***o 发帖数: 1252 | 2 It's a feature: the dtor of unique_ptr need to see the dtor of B.
So A's dtor must see B's dtor as required by it's member myptr.
【在 q****x 的大作中提到】 : The code is from stack overflow. why the dtor must be defined in .cpp, : instead of .h? Moving it to .h will lead to a compilation error. : // A.hpp : #include : class B; : class A { : std::unique_ptr myptr; : // B::~B() can't be seen from here : public: : ~A();
| q****x 发帖数: 7404 | 3 by "must see", you mean dtor of unique_ptr must be in the same compilation
unit as B's dtor does? but why the location of A's dtor matters?
【在 p***o 的大作中提到】 : It's a feature: the dtor of unique_ptr need to see the dtor of B. : So A's dtor must see B's dtor as required by it's member myptr.
| q****x 发帖数: 7404 | 4 oh, you mean the real code for A's, generated by the compiler, will contain
unique_ptr's dtor, which in turn will contain B's dtor, correct?
【在 q****x 的大作中提到】 : by "must see", you mean dtor of unique_ptr must be in the same compilation : unit as B's dtor does? but why the location of A's dtor matters?
| p***o 发帖数: 1252 | 5 yes
contain
【在 q****x 的大作中提到】 : oh, you mean the real code for A's, generated by the compiler, will contain : unique_ptr's dtor, which in turn will contain B's dtor, correct?
|
|