Sign in to follow this  

compiler complaint about pointer to member variables

This topic is 3574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to get a pointer to a member variable inside an object:
struct Object
{
	u64 field1;
	s32 field2;
	u8  field3;
	f32 field4;
	s16 field5;
};

void* ptr = &Object::field1;

The problem is that the assignment fails. Always.
error C2440: 'initializing' : cannot convert from 'u64 Object::* ' to 'void *'
1>        There is no context in which this conversion is possible
This confuses me. I don't understand why this doesn't work. I mean, an address is an address, right? It's not a pointer to a member function, where an extra parameter is hiding between lines of C. Why is it different here? I am able to feed the address directly into printf("%d") and come up with the correct value, so why is this different? And before anyone asks, this does do what I want, I'm looking for the offset of the variable into the object, not the absolute address. :D

Share this post


Link to post
Share on other sites
Object::field1 makes no sense. You cannot point to Object::field1, it has no address, it's not a variable. You can point to field1 of an instance of Object though.


Object o;
printf("%p\n", &o.field1);


Also, don't use void*. It's usually a bad idea.

Edit: Oops, didn't read you last sentence.

Share this post


Link to post
Share on other sites
Quote:
Original post by UziMonkey
Object::field1 makes no sense. You cannot point to Object::field1, it has no address, it's not a variable. You can point to field1 of an instance of Object though.

Object::field1 does makes sense, if you know what it is and how to use pointer to members. Here's a, sort of, Gamedev forum famous example of pointer to member.

edit: Forum apparently remove page-tags. Scroll down to Fruny's post...

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
This confuses me. I don't understand why this doesn't work. I mean, an address is an address, right? It's not a pointer to a member function, where an extra parameter is hiding between lines of C. Why is it different here?


Right. Except that pointers are not addresses. And also that void* is not a pointer.

The C++ language only provides an implicit conversion from a cv-qualified value pointer type T* to the bottom type void*. All other pointer types have no such conversion, and therefore attempting such a conversion is against the rules of the language. You could certainly wish that the language standard would provide a rule to handle this, just like I wish, for instance, that the standard would provide such elementary features as nested functions, anonymous functions, statements-are-expressions, typedef templates, and so on. But it isn't the case.

Share this post


Link to post
Share on other sites
I'm not entirely clear on the language you're using, but in C++, I did something like this.

// Owning class Type of variable Offset
template < class O, typename Type, Type O::*P >
struct Field
{

static Type getValue( O * owner )
{
return owner->*P;
}

static Type getValue( O & owner )
{
return owner.*P;
}

};


...

typedef Field < Object, s32, &Object::field2 > Field32;

Object o;

f32::getValue(o);


Quote:
I mean, an address is an address, right?


No, you need an offset. Even worse, the offset will change, depending on compiler and class (virtual, POD).

Quote:
It's not a pointer to a member function, where an extra parameter is hiding between lines of C.


Yep, the this pointer is still there.

Quote:
Use the "offsetof" macro.


Well, depends on style. I've found the above to be suitable for my own purposes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
I mean, an address is an address, right? It's not a pointer to a member function, where an extra parameter is hiding between lines of C. Why is it different here?

Depending on how I parse your sentence, you're right or wrong.

An address is not a pointer to a member function, so you're right.

Your code, however, is not dealing with an address but with a pointer-to-member, so you're wrong.

A pointer-to-member is not an address, whether that member is a function or a variable. The error message is pretty clear. Um, in the usual obfuscated C++ way.


--smw

Share this post


Link to post
Share on other sites

This topic is 3574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this