指向会员的指针??? [英] Pointer to member-of-member???

查看:62
本文介绍了指向会员的指针???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能为我的生活看到当

成员实际上是嵌入式结构的一部分时如何做指针成员。


也就是说,如果我有:


struct S1 {int a; };

struct S2 {S1 s; int b; };


如何在S2中获得指向a的指针?


显然我可以用b指向b >

int S2:* p =& S2 :: b;


但看似明显的语法


int S2 :: * p =& S2 :: sa;


不起作用 - 因为S2 :: sa不是/ qualified-id / - 而且

做了我能想到的其他事情。


毕竟,如果我有一个S2结构,那绝对没有区别
$ b来自编译器在s2.sa和s2.b之间的POV的$ b,那么为什么我不能

能够有一个构造s2。* p相当于前一种情况?


目前,我唯一发现的是一个相当令人讨厌的解决方法

,其中包含offsetof()宏和大量的强制转换。也就是说,


size_t offset = offsetof(S2,sa);


*(int *)((char *)& s2 +偏移)= 123;


呃: - (((


感激地收到任何更好的建议!

解决方案

Steve Rencontre写道:

我不能为我的生活看到如何做指针指向会员时
成员实际上是嵌入式结构的一部分。

也就是说,如果我有:

struct S1 {int a;};
struct S2 {S1 s; int b;};

如何在S2中获得指向a的指针?

显然我可以用b指向

int S2:* p =& S2 :: b;

但看似明显的语法

int S2 :: * p =& S2 :: sa;




void f()

{

int S1 :: * p =& S1 :: a;

S2 s2;


(s2.s)。* a = 7; // parens因为我永远无法记住。* precedence

}


Steve Rencontre写道:

我不能为我的生活看到如何做指针 - 当
成员实际上是嵌入式结构的一部分时成为成员。

也就是说,如果我有:

struct S1 {int a;结构S2 {S1 s; int b; }

如何在S2中获得指向a的指针?


好​​吧,a是一个int,所以int有什么问题??

显然我可以用b指向b
int S2:* p =& S2 :: b;


你能吗?

但看似明显的语法

int S2 :: * p =& S2 :: s.a;


对我来说不是那么明显。有没有会员功能。

不起作用 - 因为S2 :: sa不是/ qualified-id / - 而且
不做任何我能做的事情想想。

毕竟,如果我有一个S2结构,那么与s2.sa和s2.b之间的编译器POV完全没有区别,那么为什么呢?我不应该能够拥有一个构造s2。* p相当于前一种情况吗?

目前,我唯一发现的是一个相当令人讨厌的解决方法
使用offsetof()宏和大量的强制转换。也就是说,

size_t offset = offsetof(S2,sa);

*(int *)((char *)& s2 + offset)= 123;

呃: - ((

感激收到任何更好的建议!




有什么问题:


int * s2s1a =& s2.sa;


Ben Pope

-

我不只是一个数字。对于很多人来说,我被称为字符串...


Steve Rencontre写道:

我不能为我的生活看到当
成员实际上是嵌入式结构的一部分时如何做指针指向成员。

就是说,如果我有:

struct S1 {int a;};
struct S2 {S1 s; int b;};

如何获得指向a的指针在一个S2?

显然我可以用b指向b:S2:* p =& S2 :: b;

这个是不是有效的C ++语法。

但看似明显的语法

int S2 :: * p =& S2 :: sa;






类/结构的数据成员只能通过一个实例

该对象:

S2 s2;


int * p =& s2.sa ;


您是否对指向会员职能的指针感到困惑?


-

Ian Collins。


I can''t for the life of me see how to do pointer-to-member when the
member is actually part of an embedded structure.

That is, if I have:

struct S1 { int a; };
struct S2 { S1 s; int b; };

how can I get a pointer to the a in an S2?

Obviously I can point at the b with

int S2:*p = &S2::b;

But the seemingly obvious syntax

int S2::*p = &S2::s.a;

doesn''t work - because S2::s.a is not a /qualified-id/ - and neither
does anything else I can think of.

After all, if I have an S2 structure, there''s absolutely no difference
from the compiler''s POV between s2.s.a and s2.b, so why should I not be
able to have a construct s2.*p equivalent to the former case?

At the moment, the only thing I have found is a rather yucky workaround
with offsetof() macros and lots of casts. That is,

size_t offset = offsetof (S2, s.a);

*(int *) ((char *) &s2 + offset) = 123;

Ugh :-(((

Any better suggestions gratefully received!

解决方案

Steve Rencontre wrote:

I can''t for the life of me see how to do pointer-to-member when the
member is actually part of an embedded structure.

That is, if I have:

struct S1 { int a; };
struct S2 { S1 s; int b; };

how can I get a pointer to the a in an S2?

Obviously I can point at the b with

int S2:*p = &S2::b;

But the seemingly obvious syntax

int S2::*p = &S2::s.a;



void f()
{
int S1::*p = &S1::a;
S2 s2;

(s2.s).*a = 7; // parens because I can never remember .* precedence
}


Steve Rencontre wrote:

I can''t for the life of me see how to do pointer-to-member when the
member is actually part of an embedded structure.

That is, if I have:

struct S1 { int a; };
struct S2 { S1 s; int b; };

how can I get a pointer to the a in an S2?
Well, a is an int, so what''s wrong with an int*?
Obviously I can point at the b with

int S2:*p = &S2::b;
Can you?
But the seemingly obvious syntax

int S2::*p = &S2::s.a;
Not so obvious to me. There''s aren''t member functions.
doesn''t work - because S2::s.a is not a /qualified-id/ - and neither
does anything else I can think of.

After all, if I have an S2 structure, there''s absolutely no difference
from the compiler''s POV between s2.s.a and s2.b, so why should I not be
able to have a construct s2.*p equivalent to the former case?

At the moment, the only thing I have found is a rather yucky workaround
with offsetof() macros and lots of casts. That is,

size_t offset = offsetof (S2, s.a);

*(int *) ((char *) &s2 + offset) = 123;

Ugh :-(((

Any better suggestions gratefully received!



What''s wrong with:

int* s2s1a = &s2.s.a;

Ben Pope
--
I''m not just a number. To many, I''m known as a string...


Steve Rencontre wrote:

I can''t for the life of me see how to do pointer-to-member when the
member is actually part of an embedded structure.

That is, if I have:

struct S1 { int a; };
struct S2 { S1 s; int b; };

how can I get a pointer to the a in an S2?

Obviously I can point at the b with

int S2:*p = &S2::b;
This isn''t valid C++ syntax.
But the seemingly obvious syntax

int S2::*p = &S2::s.a;


?

Data members of a class/struct an only be accesses through an instance
of that object:

S2 s2;

int* p = &s2.s.a;

Are you confused with pointers to member functions?

--
Ian Collins.


这篇关于指向会员的指针???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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