由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Forward declaration with unique_ptr
相关主题
急问:compile and build dependencyC++含有指针成员的类
c++ initialize struct关于C++ STL编译的疑问
some C++ interview questionsa simple question about constructor
c++ dynamic castclass D:public B;
Help! Virtual Destructorcompiler created methods
vs2008下c++的go to definiton 直接跳到了declaration. 怎么解决?forward declaration
Interview question: is the following code OK?这个dtor为啥能被调用呢
new一定要和delete配对吗?C++ memcpy declaration use restrict keyword?
相关话题的讨论汇总
话题: dtor话题: ptr话题: unique话题: forward
进入Programming版参与讨论
1 (共1页)
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?

1 (共1页)
进入Programming版参与讨论
相关主题
C++ memcpy declaration use restrict keyword?Help! Virtual Destructor
容器中放置智能指针一问vs2008下c++的go to definiton 直接跳到了declaration. 怎么解决?
几个问题Interview question: is the following code OK?
问个面试问题,请教new一定要和delete配对吗?
急问:compile and build dependencyC++含有指针成员的类
c++ initialize struct关于C++ STL编译的疑问
some C++ interview questionsa simple question about constructor
c++ dynamic castclass D:public B;
相关话题的讨论汇总
话题: dtor话题: ptr话题: unique话题: forward