C++带有纯虚函数的模板化返回值
我有一个抽象的 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;
};
根据要求,这就是我正在做的。knownHandles
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.
};
需要与 Handle 兼容,以便它可以位于 CompatingPool 的 rootSet 中。 rootSet 也是如此。我将引导这些特殊的句柄,这样就不会出现先有鸡还是先有蛋的问题。
I have an abstract Handle<T> class that contains references an objects of type T. I want to be able to have that class be able to be converted to Handle<U>, where U is a superclass of T. I would use inheritance, but that doesn't work here. How would I go about doing this? What are good alternatives?
Example psuedo code:
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;
};
As requested, this is what I'm doing
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 needs to be compatable with Handle so it can be in the CompatingPool's rootSet. Same goes for rootSet. I will bootstrap these special handles so a chicken and egg problem does not occur.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
语言规范不允许模板虚函数。
考虑 ideone 上的这段代码,然后查看编译错误:
现在你能做什么?一种解决方案是这样的:
即在派生类中定义一个基类的
typedef
,并在Handle
中使用它。像这样的事情:或者您可以定义特征,如下所示:
在我看来,
super_traits
是一个更好的解决方案,因为您可以定义派生类的特征而不对其进行编辑。此外,您可以根据需要定义任意数量的 typedef;假设你的派生类有多个基类,你可能想要定义许多 typedef,或者最好是一个 typelist。Template virtual function is not allowed by the language specification.
Consider this code at ideone, and then see the compilation error:
Now what can you do? One solution is this:
That is, define a
typedef
of the base class in the derived class, and use it in theHandle
. Something like this:Or you can define traits, as:
In my opinion,
super_traits
is a superior solution, as you're defining the traits of derived classes without editing them. Also, you can define as many typedefs as you want; say your derived class has more than one base, you may want to define many typedefs, or preferably a typelist.