我想構建一個通用句柄類Moo和Kernig的加速C++(第257-257)。但是,爲了調用基類的clone
-函數,我需要使通用Handle
-類的Base
。聲明一個類模板作爲一個類的朋友
下面的例子給我的錯誤:
main.cpp:30:18: error: template argument required for ‘class Handle’
friend class Handle;
^
main.cpp:30:5: error: friend declaration does not name a class or function
friend class Handle;
^
main.cpp: In instantiation of ‘Handle<T>& Handle<T>::operator=(const Handle<T>&) [with T = Base]’:
main.cpp:42:7: required from here
main.cpp:33:19: error: ‘virtual Base* Base::clone() const’ is protected
virtual Base *clone() const { return new Base; }
是什麼使Handle
朋友Base
正確的符號?
#include <iostream>
template <class T>
class Handle
{
public:
Handle() : p(0) {}
Handle &operator=(const Handle &);
~Handle() { delete p; }
Handle(T *t) : p(t) {}
private:
T *p;
};
template <class T>
Handle<T> &Handle<T>::operator=(const Handle &rhs)
{
if (&rhs != this)
{
delete p;
p = rhs.p ? rhs.p->clone() : 0;
}
return *this;
};
class Base
{
friend class Handle; ///this fails
protected:
virtual Base *clone() const { return new Base; }
private:
int a;
};
main()
{
Handle<Base> h;
h = new Base;
return 0;
}