我有一个抽象的 Handle
示例伪代码:
template<class T>
class Handle {
public:
virtual ~Handle () {}
virtual T & operator* () const = 0;
virtual T * operator-> () const = 0;
virtual template<class U> operator Handle<U>* () const = 0; // being lazy with dumb pointer
};
template<class T>
class ConcreteHandle : public Handle<T> {
public:
explicit template<class U> ConcreteHandle (U * obj) : obj(obj) {}
virtual ~ConcreteHandle () {}
virtual T & operator* () const {
return *obj;
}
virtual T * operator-> () const {
return obj;
}
virtual template<class U> operator Handle<U>* () {
return new ConcreteHandle<U>(obj);
}
private:
T * obj;
};
按照要求,这就是我正在做的
class GcPool {
public:
virtual void gc () = 0;
virtual Handle<GcObject> * construct (GcClass clazz) = 0;
};
class CompactingPool : public GcPool {
public:
virtual void gc () { ... }
virtual Handle<GcObject> * construct (GcClass clazz) { ... }
private:
Handle<GcList<Handle<GcObject> > > rootSet; // this will grow in the CompactingPool's own pool
Handle<GcList<Handle<GcObject> > > knownHandles; // this will grow in the CompactingPool's own pool.
};
knownHandles 需要与 Handle 兼容,以便它可以在 CompatingPool 的 rootSet 中。 rootSet 也是如此。我将引导这些特殊句柄,这样就不会出现先有鸡还是先有蛋的问题。
最佳答案
virtual template<class U> operator Handle<U>* () const =0;
语言规范不允许使用模板虚函数。
考虑 this code at ideone ,然后看到编译错误:
error: templates may not be ‘virtual’
现在你能做什么?一种解决方案是:
template<class T>
class Handle {
public:
typedef typename T::super super; //U = super, which is a superclass of T.
virtual ~Handle () {}
virtual T & operator* () const = 0;
virtual T * operator-> () const = 0;
//not a template now, but still virtual
virtual super operator Handle<super> () const = 0;
};
即在派生类中定义一个基类的typedef
,并在Handle
中使用。像这样:
struct Base {//...};
struct Derived : Base { typedef Base super; //...};
Handle<Derived> handle;
或者你可以定义特征,如:
struct Base {//... };
struct Derived : Base { //... };
template<typename T> struct super_traits;
struct super_traits<Derived>
{
typedef Base super;
};
template<class T>
class Handle {
public:
typedef typename super_traits<T>::super super; //note this now!
virtual ~Handle () {}
virtual T & operator* () const = 0;
virtual T * operator-> () const = 0;
//not a template now, but still virtual
virtual super operator Handle<super> () const = 0;
};
在我看来,super_traits
是一个更好的解决方案,因为您可以在不编辑派生类的情况下定义它们的特征。此外,您可以根据需要定义任意数量的 typedef;假设你的派生类有多个基类,你可能想定义很多 typedef,或者最好是 typelist .
关于带有纯虚函数的C++模板返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6026234/