为什么我可以这样写:
class VoiceManager
{
public:
static const int mMaxNumOfVoices = 16;
Voice mVoices[mMaxNumOfVoices];
private:
};
但我不能使用这个:
class VoiceManager
{
public:
const int mMaxNumOfVoices = 16;
Voice mVoices[mMaxNumOfVoices];
private:
};
它说:“非静态成员引用必须相对于特定对象”
但是在这两种情况下,mMaxNumOfVoices
aconst
和is会在初始化之前先被mVoices
初始化(编译器遵循声明顺序,不是吗?)。
但这需要static
。为什么?
数组边界必须在编译时知道。尽管您的初始化代码写在代码中,但构造函数可以在运行时覆盖它。因此,您的非static
成员变量不是编译时常量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句