以下程序的输出始终为1 1 1。在《C++对象模型》一书中,提到这将给出偏移量。其目的也是为了找出对象的布局。但是,我对输出感到困惑。使用的编译器是g++ 4.5.2。
编辑(后续问题): 书中提到,
class Test
{
public:
float a;
float b;
float c;
};
int main()
{
float Test::*ptr = &Test::a;
float Test::*ptr1 = &Test::b;
float Test::*ptr2 = &Test::c;
cout<<ptr<<endl;
cout<<ptr1<<endl;
cout<<ptr2<<endl;
return 0;
}
输出:
1
1
1
编辑(后续问题): 书中提到,
origin.y = 0
可以转换为 &origin + (Point3d::y - 1)
,其中 origin 是 Point3d 对象,y 是 Point3d 类的成员变量。但是当我编译时会出现编译错误。
cout
没有为成员指针重载插入运算符,所以ptr
会被隐式转换为bool
。 - Mr.Anubis