[英]boost shared_ptr interface/object
假設我有一個API,其中有2種方法可以相互結合使用:
boost::shared_ptr<IAsset> getAsset( const std::string & id );
void update( const Asset & asset );
我在另一個類中有一個boost::shared_ptr<Asset> asset
的成員變量。 如果我做:
asset = otherObject->getAsset("first_asset");
// do some stuff
otherObject->update( *asset );
我收到以下錯誤:
\boost\include\boost/smart_ptr/shared_ptr.hpp(378): error C2440: '<function-style-cast>' : cannot convert from 'boost::shared_ptr<T>' to 'boost::shared_ptr<T>'
with
[
T=IAsset
]
and
[
T=Asset
]
No constructor could take the source type, or constructor overload resolution was ambiguous
src\TestMethod.cpp(35) : see reference to function template instantiation 'boost::shared_ptr<T> &boost::shared_ptr<T>::operator =<IAsset>(boost::shared_ptr<IAsset> &&)' being compiled
with
[
T=Asset
]
\boost\include\boost/smart_ptr/shared_ptr.hpp(378): error C2228: left of '.swap' must have class/struct/union
我在這里應該注意, Asset
類的定義確實像在class Asset : virtual public IAsset {...}
一樣擴展了IAsset
class Asset : virtual public IAsset {...}
有人可以告訴我執行此操作的正確方法,因為我無法訪問基礎API?
這是一個潛在的不安全轉換,因此您必須使用動態指針強制轉換。
(請注意,您不能使用虛擬基類中的static_cast
。)
我認為您對繼承的使用是不正確的。 如果update
需要Asset
則您需要向其發送Asset
或其子類。 IAsset
是超類。
因為您要將IAsset
到需要IAsset
的Asset
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.