在阅读深入探索c++对象模型时,介绍类数据成员的指针,以及类数据成员的偏移量。进行了一下实验
实验代码:
#include <iostream>
using namespace std;
class Point3d
{
public:
virtual ~Point3d()
{
}
void print() //这个函数的作用是答应对象的数据成员的地址
{
cout << "this=" << this << endl
<< "&Point3d::x = " << &x << endl
<< "&Point3d::y = " << &y << endl
<< "&Point3d::z = " << &z << endl;
//pritf("&Point3d::x = ",);
}
private:
public:
static Point3d pointer;
public:
float x, y, z;
};
int main(void)
{
Point3d pp;
pp.print();
cout << "&Point3d::x = " << &Point3d::x << endl
<< "&Point3d::y = " << &Point3d::y << endl
<< "&Point3d::z = " << &Point3d::z << endl; //这个cout语句,我是想打印出类的数据成员的偏移量,但是结果却都是1
//下面的printf都是打印类的数据成员的偏移量
printf("&Point3d::x = %p\n", &Point3d::x);
printf("&Point3d::y = %p\n", &Point3d::y);
printf("&Point3d::z = %p\n", &Point3d::z);
printf("&Point3d::x = %d\n", &Point3d::x);
printf("&Point3d::y = %d\n", &Point3d::y);
printf("&Point3d::z = %d\n", &Point3d::z);
return 0;
}
输出结果如图片所示。令我困惑的是,printf输出结果正常。但为什么使用cout输出的都是1。路漫漫其修远兮,我还在探索,或是因为用<<输出中如果遇到不认识的类型一率调参数为bool类似的,所以总输出1。希望知道的可以简答一下。