指向成员变量的指针
Pointer to member variable
以下程序输出始终为 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)
其中原点是 Point3d 的对象,y 是类 Point3d 的成员变量。虽然当我编译它给了我编译错误。
您不能打印指向成员的指针,但指向成员的指针可以隐式转换为 bool
,当然,可以打印这些指针。空指针转换为 false
,所有其他指针转换为 true
。默认情况下,std::cout
打印false
为0
,true
打印为 1
。
你写道,你想找到内存偏移量。虽然 FredOverflow 写的是完全正确的,但如果你想知道 a
、 b
和 c
的地址,你应该Test
你的类做一个实例。例如:
Test t;
float *ptr = &t.a;
float *ptr1 = &t.b;
float *ptr2 = &t.c;
在我的机器上,这会产生以下三个地址:
0x7fff564f8918
0x7fff564f891c
0x7fff564f8920
您会注意到它们相距 4 个字节(或 sizeof(float)
个),Test
的大小为 12 个字节(使用 sizeof(Test)
)。此外,&t
的地址0x7fff564f8918
&t.a
的相同地址。这就是类Test
实例的内存布局的形成方式。
还可以使用 offsetof()
查找POD
类型成员的偏移量。
cout << offsetof(Test, a) << endl;
cout << offsetof(Test, b) << endl;
cout << offsetof(Test, c) << endl;
收益 率
0
4
8
请注意,offsetof(Test, b)
本质上与
(unsigned long long) &(((Test*) 0)->b) - (unsigned long long) (Test*) 0
回答您的后续问题:
由于前面提到的相同错误,该代码将不起作用。但是,如果要计算origin
y
成员的地址并为其分配值0
可以这样完成:
class Point3d {
public:
float x, y, z;
};
Point3d origin;
origin.y = 10;
// We take the address of origin, which points to the first member,
// then add the offset to the member y.
float *ptr = (float*) ((unsigned long long) &origin + offsetof(Point3d, y));
cout << "Old value: " << *ptr << endl;
*ptr = 0;
cout << "New value: " << *ptr << endl;
生成输出:
Old value: 10
New value: 0
再次记住,这只能Point3d
因为POD
类型。
相关文章:
- 指针变量在 cout 函数中不起作用
- 如何将指针变量作为引用参数传递?
- 有没有办法在同名类 (c++) 中为对象分配一个指针变量
- 我试图了解在异或操作后指针变量正在更改
- 调用带有指针对象错误的指针变量
- 如何在 cuda 中将结构的指针变量从主机复制到设备
- 非指针变量和类成员上的新放置
- 线路抑制状态错误 C4703 可能未初始化的局部指针变量"back"已使用
- 全局外部指针变量在 DLL 中不可见
- 如何访问在 c++ 中在类内声明的结构类型指针变量?
- C++ 中指针变量的内存释放
- 使用指针变量打印特定的数组变量
- 如何使用构造函数初始化内存地址(指针变量)?
- 如何释放为指针变量本身提供的内存?
- 如果地址已知,如何获取指针变量的名称
- 如何处理参数中的基类和指针变量
- 指针变量 在数组中定位零
- 隐式指针变量,在自动定义中具有另一个指针变量
- 为什么可以将Char指针变量初始化为字符串,而INT指针变量不能初始化到整数数组
- 错误 C4703 可能未初始化的局部指针变量'y'使用