【发布时间】:2019-08-29 20:34:37
【问题描述】:
我做出以下声明:
class Servo {
protected:
static const int maxServos = 16;
static Servo servos[maxServos]; //Array declaration
};
Servo Servo::servos[Servo::maxServos]; //Array definition
...它可以编译,这很棒!但是我不明白它为什么会编译,因为在我看来 maxServos 是受保护的,并且在定义数组时我在全局范围内使用它。我尝试在另一个全局上下文中使用它,确实出现了编译错误:
int main() {
std::cout << Servo::maxServos; //This will not compile.
}
那么发生了什么?数组的整个定义是否以某种方式由限定数组的命名空间限定?是编译器故障吗?
我在使用 Lubuntu 16.04 O/S 的 Raspberry PI 上使用 g++ -std::c++11。
【问题讨论】:
标签: c++ c++11 language-lawyer access-control static-variables