简体   繁体   English

通过C ++中的参数定义模板继承

[英]Defining template inheritance by the arguments in c++

I have a polymorphic template MyClass that takes argument T. 我有一个采用参数T的多态模板MyClass。

template<class T>
class MyClass {...};

I would like to force an inheritance without specifing it in every derieved class, something like: 我想强制继承而不在每个已删除类中指定它,例如:

template<class T, class U : public T>
class MyClass<U> : public MyClass<T>;

Is there a way to do it? 有办法吗?

EDIT: For Example If I have 编辑:例如,如果我有

class Base {...};
class Derieved : public Base {...};

Then if the compiler creates MyClass<Derieved> then it would add 然后,如果编译器创建MyClass<Derieved>则它将添加

class MyClass<Derieved> : public MyClass<Base>

without specifing it every time I use MyClass with inheritance. 每次我在继承中使用MyClass时都没有指定它。

I want to be able to create MyClass with any parameter, but when the parameter inherits from some other class, so will the template. 我希望能够使用任何参数创建MyClass,但是当参数继承自其他类时,模板也将继承。

You want to automatically retrieve the base classes of an object. 您要自动检索对象的基类。 Sadly, it won't be possible before at least C++17, when compile-time reflection is integrated. 可悲的是,至少在C ++ 17之前,集成了编译时反射时才不可能实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM