Typedef是一个具有特定构造函数参数的类
Typedef a class with specific constructor argument
让我们从C++中的一个简单类开始:
class aClass {
bool b;
aClass(bool x){b=x;}
};
是否可以键入两个新类型stateTrue和stateFalse,这样如果我这样做:
stateTrue variable;
它将被翻译为:
aClass variable(true);
继承的替代方案可以是使aClass
成为template
:
template <bool T>
class aClass
{
public:
bool b;
aClass(): b(T) {}
};
typedef aClass<true> stateTrue;
typedef aClass<false> stateFalse;
您可以导出:
class stateTrue: public aClass {
public:
stateTrue() : aClass(true) {}
};
最接近的是
class stateTrue : // new type needed, not just a new name
public aClass { // but obviously can be converted to aClass
public: stateTrue() : aClass(true) { } // Default ctor sets aClass base to true
};
相关文章:
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在按值调用 (c++) 中转发构造函数参数
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- 如何在构造函数参数中初始化"std::set"?
- 何时应在构造函数参数中使用 const C++?
- 使用模板化结构作为构造函数参数
- 使用 lambda 作为构造函数参数是否需要C++ 17?
- 如何使输入文本文件成为构造函数参数?c++
- shared_ptr构造函数参数是否应按值传递
- 是否允许使用初始值设定项列表将const数组引用实例化为构造函数参数
- 复制构造函数参数为0
- 用作成员构造函数参数的函数的求值顺序
- 结构中的默认成员值或默认构造函数参数
- C++将引用成员绑定到构造函数参数
- 如何通过可变参数模板将多个构造函数参数转发到数组初始值设定项列表?
- 模板函数指针参数与构造函数参数
- 如何将 std::string 作为构造函数参数传递,并将其保存的 C 字符串存储在 void 指针中?
- 如何基于构造函数参数模板化类成员函数的代码
- 将派生类构造函数参数传递给受保护的成员
- 如何根据构造函数参数使用超类类型初始化成员变量?