【发布时间】:2018-12-21 13:09:14
【问题描述】:
我需要一个变通方法或一个很好的解决方案来初始化基类和子类中的一堆常量类变量
问题很简单,我得到了一个有两个构造函数的基类,子类中有两个相同的构造函数
class BASE {
int a;
int func(float x) { // just a dummy to show that this function
return (a = (int) x) + 2; // modifies a
}
public:
const int b;
BASE(const int b) : b(b) {} // doesn't use a
BASE(const float x) : a(0), b(func(x)) {}
};
struct SUB: public BASE {
const int c;
const int d;
SUB(const int b) : BASE(b), c(b), d(c + 3) {}
SUB(const float x) : BASE(x), c(b), d(c + 3) {}
};
子类需要从 BASE 中调用构造函数来初始化 BASE 中的类变量,然后子类初始化剩余的变量
到目前为止一切都很好,但问题是 SUB 的两个构造函数的功能完全相同,只是从 BASE 调用不同的构造函数
我想要这样的东西
SUB() : c(b), d(c + 3) {} // BASE() needs to be defined
SUB(const int b) : BASE(b), SUB() {}
SUB(const float x) : BASE(x), SUB() {}
但这不起作用,因为“对委托构造函数的调用应该是唯一的成员初始化器”...
将初始化列表之外的所有内容移出不起作用,因为这些是 const 类变量
【问题讨论】:
标签: c++ constructor initialization superclass class-variables