程序设计语言

数据成员指针

2009年4月6日 阅读(30)

#include <iostream>
#include <cstdio>
 
class Point3d{
public:
    virtual ~Point3d(){};
    //…
public://如果换成private或者protected,则报错
    static Point3d origin;
    float x;
    float y;
    float z;
};
 
int main()
{
    printf("&Point3d::x = %p\n", &Point3d::x);
    printf("&Point3d::y = %p\n", &Point3d::y);
    printf("&Point3d::z = %p\n", &Point3d::z);
  
   std::cout<<"&Point3d::x = "<<&Point3d::x<<std::endl;
    std::cout<<"&Point3d::y = "<<&Point3d::y<<std::endl;
    std::cout<<"&Point3d::z = "<<&Point3d::z<<std::endl;
 
    return 0;
}

输出:
&Point3d::x = 00000001
&Point3d::y = 00000005
&Point3d::z = 00000009
&Point3d::x = 1
&Point3d::y = 1
&Point3d::z = 1

特殊的地方在于:cout的输出全部上1,原因上发生了想bool类型的转换

You Might Also Like