【发布时间】:2016-09-26 00:20:35
【问题描述】:
我在构建不可复制对象时遇到问题。让我们考虑以下示例:
class Uncopyable{
protected:
Uncopyable(){};
~Uncopyable(){};
private:
Uncopyable(const Uncopyable&);
Uncopyable& operator=(const Uncopyable&);
};
class Base { };
class Derived : public Base { };
class A : private Uncopyable {
public:
A(std::map<std::string, std::shared_ptr<Base>> & inMap);
private:
A(const A&);
A& operator=(const A&);
};
int main() {
std::map<std::string, std::shared_ptr<Derived>> lMap;
std::shared_ptr<A> oA(std::make_shared<A>(lMap));
}
如果我假设我的对象 A 是 non-copyable ,它就不起作用。作为指针,我希望我的对象 A 理解 Derived 是一个 Base,但是我收到以下消息:
error C2664: 'A::A(const A &)' : cannot convert argument 1 from 'std::map<std::string,std::shared_ptr<Derived>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' to 'std::map<std::string,std::shared_ptr<Base>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &'
1> with
1> [
1> _Kty=std::string
1> , _Ty=std::shared_ptr<Derived>
1> ]
1> and
1> [
1> _Kty=std::string
1> , _Ty=std::shared_ptr<Base>
1> ]
谢谢。
【问题讨论】:
标签: c++ dictionary copy shared-ptr copy-constructor