Max Kukartsev

Hello,

Let's take for example the following code:

Code Block

class X {

public:

int i;

void MFunc() { }

};

// Declare an instance of X:

X x;

// To declare a pointer to field i within class

// X, I could use the pointer to member syntax:

int X::*imptr = &X::i;

// However, I could also bind the pointer directly

// to an instance of the class, x:

int* iptr = &x.i;

// In terms of accessing i within an instance of X,

// the two semantics achieved the same effect;

// iptr and &(x.*imptr) point to the same address

// So now, what about pointers to member functions

// I could again use the member function syntax, as

// in the previous example:

void (X::*MFunc)() = &X::MFunc;

// But why can't I bind a regular function pointer

// directly to x, an instance of X, like I did before

void (*fptr)() = &x.MFunc; // error C2276

error C2276:'&': illegal operation on bound member function expression

Thanks in Advance

Max Kukartsev



Re: Visual C++ Language Pointer to member function problem: cannot bind regular function pointer directly to instance of object

einaros

A function pointer cannot hold the instance. With pure pointers you can only retrieve a member function pointer, then execute this on an instance.

If you wish to hold an instance as well, you should use a function object, such as Boost Function (www.boost.org).





Re: Visual C++ Language Pointer to member function problem: cannot bind regular function pointer directly to instance of object

Max Kukartsev

Ok, but then how can a pointer to a member variable (field) "hold" the instance as demonstrated in the example
It would make sense of so because it is an offset into the address of the instance.
Oh, but here's the catch, isn't it But still, you could use a member function pointer, so the storage for functions is also offset within the object, right
Anyway, is there a reason that a field pointer can be used to point to a field of a particular instance while a regular function pointer can't

Thank you




Re: Visual C++ Language Pointer to member function problem: cannot bind regular function pointer directly to instance of object

Bojangles

Member function pointers can't be dereferenced directly, they have to be called by an object.
A member function can indirectly call other member functions of a class, this means the object must be known one way or another.

A variable is just storage, as long as the type is known it can be used.

Member function pointers are a special case in c++.





Re: Visual C++ Language Pointer to member function problem: cannot bind regular function pointer directly to instance of object

einaros

Max Kukartsev wrote:
Ok, but then how can a pointer to a member variable (field) "hold" the instance as demonstrated in the example


Variables are something quite different. They exist in each instance of the class, with unique addresses. Member functions, on the other hand, are not part of the instance per se. One common function body is shared among all class instances. The only thing that sets them apart, when called from the different instances, is a hidden instance (this) parameter.

Again, stick with functors (function objects), such as Boost Function. They are quite pleasant to work with.