深入探索c++对象模型中Data Members的指针

在阅读深入探索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。希望知道的可以简答一下。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值