By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

static member functions and invalid access question

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* pa*********@gmail.com:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
offsetof only works on POD and most definitely does not work inside a
static member function.
These are not errors, they are warnings.
But unsurprisingly these warnings warn of fatal crashes at runtime :)
Use the standard offsetof macro, or better yet, don't
use that kind of unnecessary low-level stuff.


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

Jul 23 '05 #3

P: n/a
>>offsetof only works on POD and most definitely does not work inside a
static member function.


OK - I lied. Now it works and gives the correct results - albeit with
the same warnings. I shall never understand C++... :]

Thanks!

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.