473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

static member functions and invalid access question

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
3 3934
* 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
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
>>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
4
by: MaxMax | last post by:
Now... I have a problem... It's an engineering problem. I have a function, we will call it MyBigFunc. It's a function that can be easily built as a static method, because it is the only function...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
10
by: shanknbake | last post by:
I'm getting the following compile-time error: error C2352: 'Person::getCount' : illegal call of non-static member function Here is my getCount function declaration:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.