zwol: stylized sketch of a face in profile (Default)
[personal profile] zwol

Is it possible in C++ to declare and initialize a pointer-to-member that points to a data member of a data member? Concretely, given

struct A { int x; int y; };
struct B { A i; A ii; A iii; A iv; };

I would like to be able to do something like this:

TYPE piix = INITIALIZER<B, ii, x>;
...
B b;
int x_within_ii = b.*piix;

Is this possible? If so, exactly how do you write TYPE and INITIALIZER? If not, why the hell not?

Date: 2008-09-03 02:24 pm (UTC)
From: [identity profile] falsedrow.livejournal.com
It's not possible. If it were, I presume it would look like this:

int B::* piix = &B::ii.x;
B b;
int x_within_ii = b.*piix;

But ii is not allowed in INITIALIZER so you're out of luck. I'm guessing it would just have complicated the grammar too much; &B::ii is a qualified-id and those are supposed to refer to direct members of the qualifying scope. I don't know if it would parse as "&B::ii" . "x" or & "B::ii.x", for one thing.

Date: 2008-09-03 10:30 pm (UTC)
From: [identity profile] zwol.livejournal.com
Drat, I was afraid that was what it came down to.

GCC's error messages for this could be improved - "invalid use of non-static data member B::ii" leaves the possibility open that if you just wrote either the type or the initializer slightly differently it would be valid...

April 2017

S M T W T F S
      1
2345678
9101112131415
16171819 202122
23242526272829
30      

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 5th, 2026 03:18 am
Powered by Dreamwidth Studios