当声明和使用静态const积分时,我发现使用我正在使用的对象引用来访问变量是方便和自然的,而不是使用类名完全限定它。我想知道这是否有不利之处?举个例子:
class MyLongClassNameIdRatherNotHaveEverywhere {
public:
static const int Len = 6;
//...
void otherInterestingThings();
void someWorkToDo();
};
int main() {
MyLongClassNameIdRatherNotHaveEverywhere *lcn = new MyLongClassNameIdRatherNotHaveEverywhere;
lcn->someWorkToDo();
cout << "the length is: " << lcn->Len << endl;
delete lcn;
return 0;
}
注意lcn->Len
...它实际上是一个常量,事实上如果lcn
为空,lcn->Len
仍然可以编译并运行得很好。我本可以在那里写MyLongClassNameIdRatherNotHaveEverywhere::Len
,这肯定会让我更明显(至少对我来说)这是一个常数。还有其他缺点吗?
答案 0 :(得分:1)
除了奇怪之外,我可以看到案例运算符的缺点 - &gt;超载了...... 范围解析运算符:: btw,不能重载。