嵌套类中的继承

Inheritance in nested class

本文关键字:继承 嵌套      更新时间:2023-10-16

我想有一个类Type,以及三个类IntegerReal和扩展Type String。是否可以将三个类嵌套在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用作标识符。