派生クラスと auto_ptr もしくは explicit
よくわからない g++ の error があります。ねぇ、教えて。
template <class T> class Base { public: typedef std::auto_ptr<Base> BasePtrType; virtual ~Base() {} virtual void Fun(BasePtrType&) = 0; }; template <class T> class Derived : public Base<T> { public: typedef typename Base<T>::BasePtrType BasePtrType; void Fun(BasePtrType& pBase) {} };
で、以下のように Fun() を呼び出すと...
typedef Derived<int>::BasePtrType BasePtr; BasePtr pOne(new Derived<int>); /* compile error: ctor with a argument が explicit されてるから */ pOne->Fun(new Derived<int>); /* compile error: BasePtr オブジェクトを渡しているのに? */ pOne->Fun(BasePtr(new Derived<int>));
error message はこれ。
main.cpp: In function 'int main()': main.cpp:45: error: no matching function for call to 'Base<int>::Fun(Derived<int>*)' main.cpp:26: note: candidates are: void Base<T>::Fun(std::auto_ptr<Base<T> >&) [with T = int] main.cpp:48: error: no matching function for call to 'Base<int>::Fun(main()::BasePtr)' main.cpp:26: note: candidates are: void Base<T>::Fun(std::auto_ptr<Base<T> >&) [with T = int]
「無名オブジェクトにして渡す行為とは暗黙の型変換にほかならない」という事なのかな?
mb2sync 『一時オブジェクト BasePtr(new Derived<int>) は 非const参照 BasePtrType& で初期化できません int& i = 3; // error』 nevil 『あ、そうか... そうですねぇ、おっしゃる通りです。穴があったら入りたい。』