Topic: pointer to member


Author: Olaf Krzikalla <Entwicklung@reico.de>
Date: Tue, 26 Feb 2002 16:22:10 GMT
Raw View
Hi,

during the last two weeks I have to work a lot with pointer to members.
The following piece of code illustrates the problems I got:

----

struct A {
  int a;
};

struct B : A {
  int b;
};

struct C {
  int c;
  B   d;
  int e[2];
};

int main()
{
  A C::* p = &C::d;  // [1] error: conversion not allowed
  int C::* q = &C::d.a;  // [2] error (only wrong syntax?)
  int C::* r = &C::e[1]; // [3] error (only wrong syntax?)
}

----

Why is [1] not allowed? And is there a syntax to achieve [2] & [3]? If
not, is there a plan to add these to C++0x?


Best regards,
Olaf Krzikalla


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Philippe A. Bouchard" <philippeb@videotron.ca>
Date: Wed, 27 Feb 2002 03:47:42 GMT
Raw View
"Olaf Krzikalla" <Entwicklung@reico.de> wrote in message
news:3C7B6162.BEC65321@reico.de...
> int main()
> {
>   A C::* p = &C::d; // [1] error: conversion not allowed
>   int C::* q = &C::d.a; // [2] error (only wrong syntax?)
>   int C::* r = &C::e[1]; // [3] error (only wrong syntax?)
> }

Pointers to member cannot point to nested members. They point to one
specific member at a time only. And if you want to have an offset of an
array, use a pointer to & C::e and another offset of type 'int' right after.
Here is an example:

C c;
c.*(& C::d).*(& B::a) = ...
* (c.*(& C::e) + 1) = ...


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Olaf Krzikalla <Entwicklung@reico.de>
Date: Wed, 27 Feb 2002 11:36:07 GMT
Raw View
Hi,

"Philippe A. Bouchard" wrote:
> "Olaf Krzikalla" <Entwicklung@reico.de> wrote in message
> news:3C7B6162.BEC65321@reico.de...
> > int main()
> > {
> >   A C::* p = &C::d; // [1] error: conversion not allowed
> >   int C::* q = &C::d.a; // [2] error (only wrong syntax?)
> >   int C::* r = &C::e[1]; // [3] error (only wrong syntax?)
> > }
>
> Pointers to member cannot point to nested members. They point to one
> specific member at a time only.
OK, as I already expected. But IMHO it's just a missing feature. Is
there a reason to not allow this in the future? I don't see anything
dangerous in the three lines above.


Best regards
Olaf Krzikalla

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Philippe A. Bouchard" <philippeb@videotron.ca>
Date: Wed, 27 Feb 2002 19:34:15 GMT
Raw View
"Olaf Krzikalla" <Entwicklung@reico.de> wrote in message
news:3C7CBECA.70F5643F@reico.de...
> > Pointers to member cannot point to nested members. They point to one
> > specific member at a time only.
> OK, as I already expected. But IMHO it's just a missing feature. Is
> there a reason to not allow this in the future? I don't see anything
> dangerous in the three lines above.

Well, the compiler would have to build a list a completely different types
with each offsets and I don't think the complexity implied is worth it.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]