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:rintOffsets()':
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.
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:rintOffsets()':
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.