コンストラクタ(B
)の定義をヘッダB.h
に移動しない限り、コードはファイルとして機能します。Pimpl with unique_ptr:インターフェイスのコンストラクタの定義を ".cpp"に移動する必要があるのはなぜですか?
B.h
class Imp; //<--- error here
class B{
public:
std::unique_ptr<Imp> imp;
B(); //<--- move definition to here will compile error
~B();
//// .... other functions ....
};
B.cpp
#include "B.h"
#include "Imp.h"
B::B(){ }
~B::B(){ }
Imp.h
class Imp{};
M ain.cpp(私をコンパイル)
#include "B.h"
Error: deletion of pointer to incomplete type
Error: use of undefined type 'Imp' C2027
私は何とかImp
のdestructureが呼ばれるかもしれないので、デストラクタは、.cpp
に移動しなければならないことを理解することができます -
delete pointer-of-Imp; //something like this
しかし、私はなぜこのルールもコンストラクタをカバーしています(質問)。
私が読んでいる: -
- Deletion of pointer to incomplete type and smart pointers
はデストラクタが.cpp
にする必要が理由を説明しています。 - std::unique_ptr with an incomplete type won't compile
は、デフォルトのデストラクタについて警告します。
重要なのはデストラクタ*です。 –
@Kerrek SBコンストラクタは重要ではありませんか? – javaLover
OT: 'B'の中に' Imp'を入れます。 'class {public:' == 'struct {' –