在编译时查找类成员的偏移量 [英] finding offset of a class member at compile time
问题描述
有没有办法在编译时找到类成员的偏移量。
例如A类{int i; int j; char c; };
这里c = 8字节的偏移量来自A
的对象的开头(假设为4字节int)。是否可以在编译时完成。
提前致谢
Hi,
Is there a way to find the offset of a class member at compile time.
e.g. class A{ int i; int j; char c; };
Here the offset of c = 8 bytes from the start of an object of A
(assuming 4 byte int). Can it be done at compile time.
Thanks in advance
推荐答案
Rahul写道:
Rahul wrote:
有没有办法找到班级的偏移量成员在编译时。
例如A类{int i; int j; char c; };
这里c = 8字节的偏移量来自A
的对象的开头(假设为4字节int)。可以在编译时完成。
提前付款
Hi,
Is there a way to find the offset of a class member at compile time.
e.g. class A{ int i; int j; char c; };
Here the offset of c = 8 bytes from the start of an object of A
(assuming 4 byte int). Can it be done at compile time.
Thanks in advance
你试过offsetof()吗?
Have you tried offsetof() ?
11月14日下午6:31,Geo < g ... @ remm.orgwrote:
On Nov 14, 6:31 pm, "Geo" <g...@remm.orgwrote:
Rahul写道:
Rahul wrote:
Hi,
有没有办法在编译时找到类成员的偏移量。
eg A类{int i; int j; char c; };
Is there a way to find the offset of a class member at compile time.
e.g. class A{ int i; int j; char c; };
这里c = 8字节的偏移量来自A
的对象(假设4字节为int) 。可以在编译时完成。
Here the offset of c = 8 bytes from the start of an object of A
(assuming 4 byte int). Can it be done at compile time.
先谢谢你试过offsetof()吗?
Thanks in advanceHave you tried offsetof() ?
将做的偏移((A *)0) - > i将在运行时发生(不是
编译时间)。但是我只想在编译时知道它。
类似于& A :: i本来会更好,但那似乎也不是什么?编译时朗姆酒$ b。
offset of will do ((A*)0)->i which will happen at run time (not
compile time). But i want to know it at compile time only.
something like &A::i would have been better, but that also dosn''t seem
to rum at compile time.
Rahul skrev:
Rahul skrev:
11月14日下午6:31,Geo < g ... @ remm.orgwrote:
On Nov 14, 6:31 pm, "Geo" <g...@remm.orgwrote:
Rahul写道:
Rahul wrote:
Hi,
有没有办法在编译时找到类成员的偏移量。
eg A类{int i; int j; char c; };
Is there a way to find the offset of a class member at compile time.
e.g. class A{ int i; int j; char c; };
这里c = 8字节的偏移量来自A
的对象(假设4字节为int) 。可以在编译时完成。
Here the offset of c = 8 bytes from the start of an object of A
(assuming 4 byte int). Can it be done at compile time.
先谢谢你试过offsetof()吗?
Thanks in advanceHave you tried offsetof() ?
将做的偏移((A *)0) - > i将在运行时发生(不是
编译时间)。但我只想在编译时知道它。
offset of will do ((A*)0)->i which will happen at run time (not
compile time). But i want to know it at compile time only.
offsetof的实现细节是多少,但它将是b / b
始终是编译时的结果。你的问题是另一个问题,因为只有为POD类型定义的偏移量为
。但是你为什么要首先在
中知道这些东西呢?回答这个问题可能会让位于真正的
解决方案。
/ Peter
How offsetof does its stuff is an implementation detail, but it will
always be a compile-time result. Your problem is another as offsetof is
defined for POD types only. But why do you want to know such stuff in
the first place? Answering that question might give way to the "real"
solution.
/Peter
>
类似于& A :: i本来会更好,但是在编译时,朗姆酒似乎也不值得b $ b。
>
something like &A::i would have been better, but that also dosn''t seem
to rum at compile time.
这篇关于在编译时查找类成员的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!