我有以下情況:類「B」繼承類「A」。類「C」引用了「A」,它在構造函數中傳遞給它。在「D」類中,我想使用「C」類,但是我只提到了「B」。目前我使用標準指針,但我需要遷移到boost :: shared_ptr。創建boost :: shared_ptr與構造函數中的引用
class A {...};
class B: public A {...};
class C {
public:
C(A& _a)
: a(_a)
{...}
private:
A& a;
};
class D
{
private:
B& b;
void someFunc()
{
C* c1 = new C(b); // Working
boost::shared_ptr<C> c2 = boost::make_shared<C>(b); // not working: "cannot convert const B in A&" error
}
};
我的問題:我如何需要包裝/投/ derefrence /任何實例「B」,這樣的shared_ptr正確地創建?
通過上面的實現,我得到一個「不能轉換const B在A &」錯誤。
感謝您的支持。
這是在C++ 03語境?如果是這樣,請相應標記。我希望你的代碼可以在C++ 11中使用。 – Angew
經過測試;它可以在C++ 11和[使用C++ 14](http://ideone.com/Q6MjYi)上正常工作。 – mindriot
始終包含實際的編譯器錯誤消息。 「''不工作'」是歡鬧的無信息 – sehe