首先构造基类(如果有多个基类,从左到右),然后按照声明的顺序构造成员变量(不是在任何成员初始化列表中的顺序)。
但这仅适用于非静态成员变量。从您的示例来看,w、h 和 o 是适用于类的所有实例的常量。因此,您只需将类更改为:
class IPianoRoll : public IControl
{
private:
int x, y;
static const int w = 30 * numStep + 1;
static const int h = 8 * numSemitones + 1;
static const int o = 5;
public:
IPianoRoll(IPlugBase* pPlug, int pX, int pY)
: IControl(pPlug, IRECT(pX, pY, pX + o + w + o, pY + o + h + o))
, x(pX)
, y(pY)
{
}
};
一切都会好起来的。请注意,我已将设置 x 和 y 移到成员初始化列表中。
如果您希望变量是非静态的(以便以后可以针对类的不同实例更改它们),那么我会写如下内容:
class IPianoRoll : public IControl
{
private:
int x, y;
static const int w_default = 30 * numStep + 1;
static const int h_default = 8 * numSemitones + 1;
static const int o_default = 5;
int w = w_default;
int h = h_default;
int o = o_default;
public:
IPianoRoll(IPlugBase* pPlug, int pX, int pY)
: IControl(pPlug, IRECT(pX, pY, pX + o_default + w_default + o_default,
pY + o_default + h_default + o_default))
, x(pX)
, y(pY)
{
}
};