gpt4 book ai didi

c++ - boost shared_ptr 接口(interface)/对象

转载 作者:行者123 更新时间:2023-11-27 23:24:45 26 4
gpt4 key购买 nike

假设我有一个 API,它有 2 个方法,它们将相互结合使用:

boost::shared_ptr<IAsset> getAsset( const std::string & id );
void update( const Asset & asset );

我有一个成员变量 boost::shared_ptr<Asset> asset在另一个类(class)。如果我这样做:

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 的类定义确实扩展了 IAssetclass Asset : virtual public IAsset {...}

由于我无法访问底层 API,有人可以告诉我执行此操作的正确方法吗?

最佳答案

这是一个潜在的不安全转换,因此您必须使用 dynamic pointer cast .

(请注意,您不能使用虚拟基类中的 static_cast。)

关于c++ - boost shared_ptr 接口(interface)/对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10175369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com