pointer to a member function

Class TheClass
{
void function foo();
{
}
}

TheClass aClass;
TheClass bClass;


void function *pfoo();

void function fooUsing(void function *pfoo());

fooUsing (aClass.foo);


Okay, pfoo will contain the address of the function TheClass::foo right?

That is the same address whether we put

aClass.foo or bClass.foo

So how do fooUsing "know" which class function to call? aClass.foo or bClass.foo?

Some says that a pointer to the class itself is passed on to a function.

But that means fooUsing will be using a function that takes parameter of type TheClass and not a no parameter.
Okay, pfoo will contain the address of the function TheClass::foo right?


Well the code you posted is not quite C++ and would not compile... but in theory, yes.

That is the same address whether we put

aClass.foo or bClass.foo

So how do fooUsing "know" which class function to call? aClass.foo or bClass.foo?


That's exactly why you don't do it this way. You pass a pointer to a function, but in order to call the function you still need an object... so you would have to pass something else to fooUsing.

Some says that a pointer to the class itself is passed on to a function.


This is correct. Non-static member functions have a hidden, implicit 'this' pointer passed to them. That pointer is the pointer to the object they're referencing.

But that means fooUsing will be using a function that takes parameter of type TheClass and not a no parameter


Yes and no.

Yes because behind the scenes, C++ will pass the this pointer the same way it passes function parameters.

No because you don't pass the object as a pointer, but rather you just have different syntax.

Here's how you would do your example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Class TheClass
{
public:
  void foo() { }
};

void fooUsing(
    void (TheClass::*func)(),  // the syntax for a pointer to a member function
    TheClass& obj )         // the object with which to call the function on
{
  (obj.*func)();  // call the function with 'obj' as the object
}

int main()
{
  TheClass aClass;

  fooUsing( &TheClass::foo, aClass );  // give it the function pointer, and the object
}



Of course this is horrendously ugly and hard to remember how to do (I had to look it all up -- I couldn't remember the syntax offhand). Which is just another reason to avoid using function pointers. C++ provides alternatives that are generally much easier to use and a lot safer.
Last edited on
In VB.net, if fooUsing accepts a pointer to a function under a certain criteria, you can pass on any function that fit that criteria.

Whether that function is a member function or a normal function doesn't seems to matter.

I was just wondering if member functions have a different address than the normal function.

Soo

&aClass.foo != bClass.foo;

You said it can't be done.

So fooUsing need to specify that it wants a parameter func where func has to be a member of TheClass class. You sure?
In VB.net, if fooUsing accepts a pointer to a function under a certain criteria, you can pass on any function that fit that criteria.


The same is true of C++.. it just has a different way of doing it.

The only thing is.... void f() and void MyClass::f() do not match. They don't meet the same criteria. The latter takes a hidden MyClass object as a parameter. So you can't use them interchangably.

Whether that function is a member function or a normal function doesn't seems to matter.


It does matter in C++.

But note that there are ways around this and ways to get the behavior you want (take a look at boost::bind and related functions). They get pretty complicated though. And again this is something that generally is to be avoided if possible.

&aClass.foo != bClass.foo;


Well the address of the actual foo function is the same for each object. The problem if you need an object to act as the 'this' pointer in the function.

So fooUsing need to specify that it wants a parameter func where func has to be a member of TheClass class. You sure?


I don't really understand your summary here.

But yes I'm sure that everything I've said in this thread is true.
Topic archived. No new replies allowed.