c++/1841: Incorrect error: invalid use of member '' in static member function
Jean-Bernard Delva
JeanBernard.Delva@trisignal.com
Fri Feb 2 16:02:00 GMT 2001
> In my experience MSVC compiling something tells you very little about
> its correctness, unless you're talking about -ve correlations :-)
Agreed.
That is why I compiled it with the Diab compiler (a well-repected compiler
for embedded systems) and I get the same results.
> > struct X
> > {
> > static int X::* foo () { return &x; };
> >
> > int x;
> > };
>
> Maybe you could explain what this means? I don't think it has a
> meaning. If a member function is static, it applies to the class as a
> type, and not any specific instance. So the 'x' of which instance are
> you trying to return a pointer to?
I understand that this construct is bordering on the isoteric but we need
code that uses this. And after analysis, it seems correct.
Since the static function foo returns a pointer to the data member x, it is
only an offset into the structure. And this offset cannot be used without a
structure instance. So a 'this' isn't really necessary in this case.
Ex:
int main ()
{
int X::* p, x;
X Object;
p = X::foo ();
x = Object.*p;
return 0;
}
If I understand pointer to members correctly, this is the way to use them
(.* and ->*). That is why I think our construct is ok and safe in a static
function. Other compilers seem to think so.
I hope this makes my case more clear.
Jean-Bernard Delva
More information about the Gcc-bugs
mailing list