コピーできないオブジェクトを作成する際に問題があります。次の例を考えてみましょう:コピー可能なオブジェクト、マップ、shared_ptr:コピーコンストラクタのエラー
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は、非コピー可能であることを前提とした場合、それはwork.Asポインタをしない、私は、オブジェクトAが派生が基本であることを理解することが予想されるが、代わりにI次のメッセージが表示されます:
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> ]
ありがとうございます。
私はあなたが指摘して参照してください、それは非常に明確です。 関数fが引数としてstd :: shared_ptrをとり、std :: map >マップを定義します。私はlMap [0]にstd :: shared_ptr を割り当てます。 lMap [0]をfに使用することはできません。 –
Canardini
@Canardini 'f'が' shared_ptr 'を受け取った場合、' shared_ptr 'をそれに渡すことはできません。実際に 'Derived'オブジェクトを指していることをどのように知っていますか? –
Barry
そうですね、これはあなたの答えから私が理解したものです。私は動的キャストポインタを使用しようとしましたが、問題を解決できませんでした。 – Canardini