c++ - 嵌套类中的继承

标签 c++ inheritance nested

我想要一个类 Type 和三个类 IntegerRealString 扩展类型。是否可以将这三个类嵌套在 Type 中?这样我就可以编写 Type::Integer int 并获取一个 Type::Integer 类型的对象,该对象继承自 Type

class Type {
   class Integer : public Type {

   };
   class Real : public Type {

   };
   class String : public Type {

   };
};

Type::Integer int;

最佳答案

在不考虑设计的合理性的情况下:

class Type {
public:
   class Integer;
   class Real;
   class String;
};

class Type::Integer : public Type{};
class Type::Real    : public Type{};
class Type::String  : public Type{};

旁注:您不能使用 int 作为标识符。

关于c++ - 嵌套类中的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28984319/

相关文章:

c++ - 返回不同的子类型C++

c++ - 如何在检测到人脸opencv后扩展矩形

c++ - Win32 C/C++ 检查同一程序的两个实例是否使用相同的参数

c++ - 你能在 C++ 中使用 'new' 模拟动态数组大小吗?

Java 通用 : Can a Generic extends another Generic class?

css - 为什么我的 div 不能并排 float ?

C++ - 为任何 lambda 创建实例化桶

C++父类(super class)和子类

xml - 如何使用模板遍历嵌套的 XML 结构

c - 将结构数组嵌套到另一个结构中