以下三个课程:
class Base
{
public:
int var1;
};
class Base2
{
public:
int var2;
};
class Derive:public Base,public Base2
{
public:
int var3;
};
int main()
{
printf("%d %d %d %d %d", &Base::var1, &Base2::var2, &Derive::var1, &Derive::var2, &Derive::var3);
int Derive::* p = &Derive::var1;
Derive d;
d.var1 = 2;
printf("%d", d.*p);
}
输出结果为0 0 0 0 8 2
。我对&Derive::var1
和&Derive::var2
的结果感到困惑。为什么他们分别是0
而不是0
和4
?
注意:我用gcc4.7.1和vs2010测试它。结果是一样的。
答案 0 :(得分:0)
@HerbSutter在Address of C++ pointer to class data member in Visual Studio中回答:
&Derive::var1
的类型为int Base::*
而不是int Derive::*
。
&Derive::var2
的类型为int Base2::*
而不是int Derive::*
。