 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Steve Rencontre Guest
|
Posted: Thu Feb 16, 2006 12:06 am Post subject: Pointer to member-of-member??? |
|
|
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! |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Thu Feb 16, 2006 1:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
Steve Rencontre wrote:
| Quote: | 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. |
| Quote: | 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. |
|
| Back to top |
|
 |
Ben Pope Guest
|
Posted: Thu Feb 16, 2006 1:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
Steve Rencontre wrote:
| Quote: | 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*?
| Quote: | Obviously I can point at the b with
int S2:*p = &S2::b;
|
Can you?
| Quote: | But the seemingly obvious syntax
int S2::*p = &S2::s.a;
|
Not so obvious to me. There's aren't member functions.
| Quote: | 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... |
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Thu Feb 16, 2006 1:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
Steve Rencontre wrote:
| Quote: | 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
} |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Thu Feb 16, 2006 2:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
"red floyd" <no.spam (AT) here (DOT) dude> wrote in message
news:AFPIf.57079$PL5.55370 (AT) newssvr11 (DOT) news.prodigy.com
| Quote: | 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
}
|
You mean
(s2.s).*p = 7;
--
John Carson |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Thu Feb 16, 2006 2:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
"Ben Pope" <benpope81_REMOVE_ (AT) gmail (DOT) com> wrote in message
news:43f3c60d$0$8245$6d36acad (AT) taz (DOT) nntpserver.com
| Quote: | 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*?
|
If you have only one S1 object, then probably nothing. If you have 1,000,
then you would need 1,000 pointers. With a pointer to member, by contrast,
you only need one pointer to member.
| Quote: | Obviously I can point at the b with
int S2:*p = &S2::b;
Can you?
|
Yes, he can.
| Quote: | But the seemingly obvious syntax
int S2::*p = &S2::s.a;
Not so obvious to me. There's aren't member functions.
|
That particular syntax is wrong, but you can have pointers to data members,
just as you can have pointers to member functions. A pointer to member is
essentially an offset into the class, e.g.,
struct Foo
{
int x, y;
};
int Foo::*px = &Foo: ;
int Foo::*py = &Foo::y;
Now, given:
Foo f1;
you can use
f1.*px and f1.*py
in place of
f1.x and f1.y
respectively. More significantly, given:
Foo array[1000];
you can use
array[i].*px and array[i].*py
for i=0 to 999 in place of
array[i].x and array[i].y
respectively. What is the point? Suppose you wanted to swap the roles of x
and y (because, e.g., they represent coordinates and you are doing a
coordinate transformation). Then you just need to change the pointer
assignments to:
px = &Foo::y;
py = &Foo: ;
and now
array[i].*px will affect the y value and array[i].*py will affect the x
value.
--
John Carson |
|
| Back to top |
|
 |
Steve Rencontre Guest
|
Posted: Thu Feb 16, 2006 2:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
Ben Pope wrote:
| Quote: | 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*?
|
It's not a pointer-to-member! |
|
| Back to top |
|
 |
Steve Rencontre Guest
|
Posted: Thu Feb 16, 2006 2:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
red floyd wrote:
| Quote: | 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
}
|
Thanks, but that's not what I want. I want a pointer to an int
/somewhere/ in S2, not one that makes me specify exactly what bit of S2
it's in. |
|
| Back to top |
|
 |
Steve Rencontre Guest
|
Posted: Thu Feb 16, 2006 2:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
Ian Collins wrote:
| Quote: | 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.
|
Whoops, missing ':'
int S2::*p = &S2::b;
But with that typo fixed, it's definitely proper C++.
| Quote: | Are you confused with pointers to member functions?
|
Pointers to data members are perfectly valid. In any case, the same
problem applies with pointers to function members. IOW, if I replaced
the declarations 'int a' and 'int b' with 'int a()' and 'int b()',
there'd still be no obvious way of specifying a pointer to member a; |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Thu Feb 16, 2006 3:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
Steve Rencontre wrote:
| Quote: | Ben Pope wrote:
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*?
It's not a pointer-to-member!
|
It is when the member is an int. You have pointers to member functions
mixed up with normal pointers.
--
Ian Collins. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Thu Feb 16, 2006 3:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
Steve Rencontre wrote:
| Quote: |
Thanks, but that's not what I want. I want a pointer to an int
/somewhere/ in S2, not one that makes me specify exactly what bit of S2
it's in.
|
That doesn't make sense, poners point to exact things.
--
Ian Collins. |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Thu Feb 16, 2006 3:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
"Steve Rencontre" <reply-to (AT) valid (DOT) address> wrote in message
news:6JidnWjxx5kjRG7enZ2dnUVZ8qKdnZ2d (AT) pipex (DOT) net
| Quote: | red floyd wrote:
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 }
Thanks, but that's not what I want. I want a pointer to an int
/somewhere/ in S2, not one that makes me specify exactly what bit of
S2 it's in.
|
Then I believe you are out of luck. You only get that sort of flexibility
with a regular pointer, not with a pointer to member.
--
John Carson |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Thu Feb 16, 2006 4:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
"Ian Collins" <ian-news (AT) hotmail (DOT) com> wrote in message
news:1140057458.767467@drone2-svc-skyt.qsi.net.nz
| Quote: | Steve Rencontre wrote:
Thanks, but that's not what I want. I want a pointer to an int
/somewhere/ in S2, not one that makes me specify exactly what bit of
S2 it's in.
That doesn't make sense, poners point to exact things.
|
It does make sense. You can define
int * ptr;
and make it point to any int (only one int at a time, of course).
In a similar way, Steve wants a pointer to member int that can be made to
point to many different int members, although only one at a time. Alas, he
wants it to be able to point to both direct members and indirect members
(i.e., members of members). This is not possible, as far as I know.
--
John Carson |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Thu Feb 16, 2006 4:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
"Ian Collins" <ian-news (AT) hotmail (DOT) com> wrote in message
news:1140057529.898447@drone2-svc-skyt.qsi.net.nz
| Quote: | Steve Rencontre wrote:
Ben Pope wrote:
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*?
It's not a pointer-to-member!
It is when the member is an int.
|
A data member that is an int can be pointed to in two ways: by a regular
pointer to int and by a pointer to a member int. You don't seem to believe
in the existence of the second category of pointers. They do exist
nevertheless. See my reply to Ben Pope.
--
John Carson |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Thu Feb 16, 2006 5:06 am Post subject: Re: Pointer to member-of-member??? |
|
|
John Carson wrote:
| Quote: |
A data member that is an int can be pointed to in two ways: by a regular
pointer to int and by a pointer to a member int. You don't seem to believe
in the existence of the second category of pointers. They do exist
nevertheless. See my reply to Ben Pope.
Oh I do, I was just a little confused by the question.... |
--
Ian Collins. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|