static member functions and invalid access question

P

paul.furber

Hi all,
I have some code which looks a bit like this:

#define Offset(m, T) ((size_t)(&((T *)1)->m) - 1)

class Point:

private:
int *x,*y;

public:
static void getOffsets();

}

void Point::getOffsets()
{

cout << Offset(x, Point) << endl;
cout << Offset(y, Point) << endl;

}

This compiles without errors and runs correctly. If I try and access
the member variables directly like so:

void Point::getOffsets()
{

cout << x << endl;
cout << y << endl;

}

then of course I get a compile time error:

In static member function `static void Point::printOffsets()':
error: invalid use of member `Point::x' in static member function

because static member functions have no `this` pointer and so can't
access normal member variables. I guess in the first version the
preprocessor does its arithmetic and just returns the offset before the
C++ compiler starts examining the legality of such a construct. Is this
understanding correct?

Now the fun begins. The Offset macro and static member functions as
defined above are both used in a very large (500 000+ lines) game
engine I'm working with at the moment. When I use gcc on amd64 in
32-bit mode (using the -m32 flag), the entire codebase compiles
correctly. When I try and compile it natively for 64-bit, I get these
sorts of errors:


warning: invalid access to non-static data member `
GuiControl::mConsoleCommand' of NULL object
gui/guiControl.cc:107: warning: (perhaps the `offsetof' macro was used
incorrectly)

What's bizarre is that some classes compile without errors under 64-bit
and others don't, despite using exactly the same Offset macro and
identical syntax in their static member functions. I can't post example
code here because its proprietary but perhaps someone can point me in
the right direction as to what's going wrong. First prize would be an
offset macro that works under 64-bit in the same way as the above one
works for 32-bit.

TIA.
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi all,
I have some code which looks a bit like this:

#define Offset(m, T) ((size_t)(&((T *)1)->m) - 1)

Use the standard offsetof macro, or better yet, don't
use that kind of unnecessary low-level stuff.

When I try and compile it natively for 64-bit, I get these
sorts of errors:


warning: invalid access to non-static data member `
GuiControl::mConsoleCommand' of NULL object
gui/guiControl.cc:107: warning: (perhaps the `offsetof' macro was used
incorrectly)

These are not errors, they are warnings.

First prize would be an
offset macro that works under 64-bit in the same way as the above one
works for 32-bit.

Use the standard offsetof macro, or better yet, don't
use that kind of unnecessary low-level stuff.
 
P

paul.furber

offsetof only works on POD and most definitely does not work inside a
static member function.

But unsurprisingly these warnings warn of fatal crashes at runtime :)

Don't I wish. It's not my codebase though.
 
P

paul.furber

offsetof only works on POD and most definitely does not work inside a
OK - I lied. Now it works and gives the correct results - albeit with
the same warnings. I shall never understand C++... :]

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top