从模板类继承,类型在子类中声明
Inheriting from a template class, Types are declared in child class
我想要一个模板父类(虚拟)。子类从父类继承并在其内部定义类型。
// PARENT
template <typename A, typename B>
class Parent
{
public:
Parent();
virtual ~Parent();
// ...
};
// CHILD
class Child : public Parent<SomeStruct , AnotherStruct>
{
public:
struct SomeStruct
{/*...*/};
struct AnotherStruct
{/*...*/};
Child();
~Child();
// ...
};
显而易见,编译器会抱怨尚未定义的"SomeStruct"。问题是如何实现类似的目标。一个简单的方法是在类之外定义结构,但这会让事情变得更加丑陋。
这是一个鸡和蛋的情况。编译器需要查看声明的结构,以便实例化Parent。
你可以试试这样的东西:
template <typename A, typename B>
class Parent
{
public:
Parent();
virtual ~Parent();
// ...
};
// CHILD
class ChildBase
{
public:
struct SomeStruct
{/*...*/};
struct AnotherStruct
{/*...*/};
};
class Child : public ChildBase, public Parent<ChildBase::SomeStruct, ChildBase::AnotherStruct>
{
public:
using ChildBase::SomeStruct;
using ChildBase::AnotherStruct;
Child();
~Child();
// ...
};
这走的是多重继承路线。或者,您可以将结构声明放在命名空间中,而不是基类中,因为基类不会将它们留在全局命名空间中。
这两种方法都不是您想要的,但如果您想要Child::SomeStruct类型的语法,则不会污染全局命名空间,并使类型在Child上可见。
您无法执行您正在尝试的操作。
可以正向声明类,但不能正向声明嵌套类。你必须重新考虑你的设计。
有一个解决方案可能会对您有所帮助。这并不完全像你正在做的事情,但它正在实现同样的事情。使用"策略"idom看看这个:
// PARENT
template <typename Policy>
struct Parent : Policy {
Parent();
virtual ~Parent();
// ...
};
// CHILD POLICY
struct ChildPolicy {
struct SomeStruct
{/*...*/};
struct AnotherStruct
{/*...*/};
}
// CHILD
struct Child : public Parent<ChildPolicy> {
Child();
~Child();
// Here you can use your two types
// ...
};
或者,如果您在Child中使用的类型在该类型的所有子级中都有一个良好的类型名称,您可以这样声明父级:
template <typename Policy>
struct Parent {
using typename Policy::SomeStruct;
using typename Policy::AnotherStruct;
Parent();
virtual ~Parent();
// ...
};
我强烈推荐父的第二个解决方案
相关文章:
- 继承期间显示未知行为的子类
- 通过指向指针数组的指针访问子类的属性
- c++类声明时,相同的例程,不同的成员变量类型
- 从父类方法返回子类对象
- 如何在C++中获取该对象的类声明中对象的地址?
- c++, 在子类中,如何在没有对象的情况下访问父类的方法?
- 将父类对象强制转换为子类的问题
- 将子类方法声明为基类的友元
- 用初始化列表和超类构造函数声明子类构造函数的正确方式
- 我们可以在没有新实例化的情况下声明一个抽象方法来返回抽象超类中的子类对象吗
- 声明基类类型的指针,但随后通过指向子类来实例化它.这是良好的编程实践吗?
- 何时应在Qt中将子对象声明为其父类的成员变量
- 我们如何有证据表明,声明虚拟函数的类是2个字节,其中一个不超过一个未声明,而在子类中
- 通过"a pointer of the base class"访问未在基类中声明的子类的方法或属性(动态)
- 如何正确声明子类?(C++)
- 为什么我需要在子类中重新声明"虚拟"方法?[C++/多态性]
- CLANG VS GCC 模板子类在父级中使用前向声明的类
- 在父类中声明并在子类中定义
- 从模板类继承,类型在子类中声明
- 前向声明子类模板