我有这个类:
class IPianoRoll : public IControl
{
private:
int x, y;
int w = 30 * numStep + 1;
int h = 8 * numSemitones + 1;
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;
}
}
但私有变量值似乎比成员init列表"晚"才可用。所以o
、w
和h
不是用我的值初始化的,并且它采用不同的值。
有没有办法预先初始化私有变量?
类成员的访问说明符不相关。与相关的是它们在类声明中的出现顺序。类成员按照的顺序初始化。
依赖类成员初始化的特定顺序是非常危险的,因为错误的重构器可能会在头文件中重新排序,而不会意识到源文件依赖于特定的顺序。
您还应该注意,读取未初始化变量时的行为是未定义的。
首先构造基类(如果有多个基类,则从左到右),然后按照声明的顺序构造成员变量(而不是任何成员初始值设定项列表中的顺序)。
然而,这只是非静态成员变量的情况。从您的示例来看,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)
{
}
};