结构的地址与其第一个字段的地址 [英] Adress of a struct vs address of its first field

查看:64
本文介绍了结构的地址与其第一个字段的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好,


假设我有一个简单,陈旧,无聊的struct foo(即没有虚拟的
函数,没有方法,只有数据字段) 。假设第一个字段是bar。


struct foo s;


将& s ==&(s.bar)永远是如果栏的类型不正确?


如果栏的类型是无符号字符[16]怎么办?


-

问候,抱怨

Hello everybody,

Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?

What if the type of bar is unsigned char[16]?

--
Regards, Grumble

推荐答案

Grumble写道:
Grumble wrote:
假设我有一个简单,陈旧,无聊的结构foo(即没有虚拟功能,没有方法,只有数据字段)。假设第一个字段是bar。

struct foo s;

无论条形类型如何,Will& s ==&(s.bar)总是如此?

如果bar的类型是unsigned char [16]怎么办?
Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?

What if the type of bar is unsigned char[16]?




你不应该这么做。但是,对于POD,您可以使用''offsetof''

宏来查找实际偏移量。你很可能会获得0美元,但是这种语言并不能保证,IIRC。当然它是

可能我不会RC。


V



You shouldn''t assume that. However, for POD you can use ''offsetof''
macro to find out what the actual offset is. You''re very likely to
get 0, but it''s not guaranteed by the language, IIRC. Of course it
is possible that I don''t RC.

V


Grumble写道:
Grumble wrote:
大家好,

假设我有一个简单,陈旧,无聊的struct foo(即没有虚拟的
函数,没有方法,只有数据字段)。假设第一个字段是bar。

struct foo s;

无论条形类型如何,Will& s ==&(s.bar)总是如此?

如果bar的类型是unsigned char [16]怎么办?
Hello everybody,

Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?

What if the type of bar is unsigned char[16]?




两者都是。这是C中的规则,对于类似C的结构(C ++

标准称为POD),同样的规则适用。


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


Pete Becker写道:
Pete Becker wrote:
Grumble写道:
Grumble wrote:
struct foo s;

无论条形类型如何,Will& s ==&(s.bar)总是如此?
如果条形类型怎么办?是unsigned char [16]?

对两者都是。这是C中的规则,对于类似C的结构(
struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?
What if the type of bar is unsigned char[16]?

Yes to both. That''s the rule in C, and for C-like structs (which the



C ++标准调用POD),同样的规则适用。


C++ standard calls PODs) the same rule applies.




实际上,我不认为上面的代码应该编译

没有诊断(这将导致编译失败

许多系统):你无法将''foo *''与''int *''进行比较。但是,下面的

应编译并为POD类型生成true:



当然,这只是挑剔...

-

< ;至mailto:二*********** @ yahoo.com> < http://www.dietmar-kuehl.de/>

< http://www.contendix.com> - 软件开发&咨询



Actually, I don''t think that the above code is supposed to compile
without a diagnostic (which will cause the compilation to fail on
many systems): You cannot compare ''foo*'' to ''int*''. However, the
following should compile and yield true for POD types:

static_cast<void*>(&s) == static_cast<void*>(&s.bar)

Of course, this is just nitpicking...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


这篇关于结构的地址与其第一个字段的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆