0
我不明白这是如何编译的。它似乎不应该,如果我在构造函数中使用非NULL值,它不会。C++`int * class :: * member = NULL`编译,为什么?
#include <stdio.h>
class MyClass{
private:
int *first;
public:
MyClass();
};
MyClass::MyClass(){
int whatever = 42;
//int* MyClass::*first = &whatever;//This does not compile
int* MyClass::*first = NULL;//This compiles
}
int main(){
MyClass doSomething;
return 1;
}
看来,通常被用于静态瓦尔,这不是这种type Class::member = value
语法。
另外,成员名称前面有一个星号,这更加令人困惑。
如果我将行切换到注释掉的行,编译器会像预期的那样抱怨。
error: cannot convert ‘int*’ to ‘int* MyClass::*’ in initialization
虽然我确实希望发生错误,但我不知道int* MyClass::*
是什么类型。或者如何使用它。
我不确定,但我怀疑你已经创建了一个指向成员变量的指针。 http://stackoverflow.com/questions/670734/c-pointer-to-class-data-member –
尝试'int * MyClass :: * first =&MyClass :: first;'。给它一个不同的名字可能会有点混乱。 –
_static vars_在哪里? – P0W