【发布时间】:2012-08-20 01:52:09
【问题描述】:
我正在玩指向成员的指针,并决定实际打印指针的值。结果出乎我的意料。
#include <iostream>
struct ManyIntegers {
int a,b,c,d;
};
int main () {
int ManyIntegers::* p;
p = &ManyIntegers::a;
std::cout << "p = &ManyIntegers::a = " << p << std::endl; // prints 1
p = &ManyIntegers::b;
std::cout << "p = &ManyIntegers::b = " << p << std::endl; // prints 1
p = &ManyIntegers::c;
std::cout << "p = &ManyIntegers::c = " << p << std::endl; // prints 1
p = &ManyIntegers::d;
std::cout << "p = &ManyIntegers::d = " << p << std::endl; // prints 1
return 0;
}
为什么p 的值总是1? p 的值不应该以某种方式反映它指向的类成员吗?
【问题讨论】: