正如MSDN庫here中所述,我想用pimpl習語進行實驗。現在我有一個Foo.hpp
與帶有pimpl習語的模板類不正確
template<typename T>
class Foo {
public:
typedef std::shared_ptr<Foo<T>> Ptr;
Foo();
private:
class Impl;
std::unique_ptr<Impl> pImpl;
};
其中T
不使用參數尚未。實現存儲在Foo.cpp
template<typename T>
class Foo<T>::Impl {
public:
int m_TestVar;
};
template<typename T>
Foo<T>::Foo() : pImpl(new Impl) {
this->pImpl->m_TestVar = 0x3713;
}
目前的編譯器有兩個錯誤,一個警告:
use of undefined type 'Foo<T>::Impl'; ... vc\include\memory in line 1150
can't delete an incomplete type; ... vc\include\memory in line 1151
deletion of pointer to incomplete type 'Foo<T>::Impl'; no destructor called; ... vc\include\memory in line 1152
什麼是這裏的concflict和我怎麼能解決它?
編輯。刪除了致電std::make_shared
- 複製&根據一箇舊版本粘貼失敗。
1.定義**必須**在頭文件中(由於模板)。 2.我沒有看到'Foo :: Foo'的定義 – 2013-03-20 12:38:07
@KirilKirov:通過移動頭文件中的所有定義,我將放棄編譯過程中的任何速度改進,並且在更改Impl時必須重新編譯頭的所有調用者因爲它是用於模板類(與來自MSDN Library的描述相比較)。我仍然應該使用pimpl成語來分離部分? – 2013-03-20 12:46:06
看看這個:http://herbsutter.com/gotw/_101/ – 2013-03-20 13:04:52