3

I have been trying to understand function pointers in C++ so that I can successfully use them in one of my projects. I am running into a logic problem though. Say we have two classes: a parent class and a child class which inherits the parent class.

class Parent{
    ...other stuff
   void (Parent::*ptr2func) ();
    ...other stuff
};

Then we have a child class:

class Child : public Parent{
       ...other stuff
       void afunc();
       ...other stuff
};

I wanted to connect the pointer of the parent class to the afunc() function of the child class. In the constructor of the child class is where I tried to do it like so:

  Child::Child()
  {
    ptr2func = &Child::afunc;
  }

This returned an expected error: cannot convert void (Child::*)() to void (Parent::*)() in assignment.

I was afraid that such a thing would happen. But how do I get over this? Can't I link a function pointer to a member function of a parent class to a member function of the child class? As you can understand I am trying to achieve polymorphism through function pointers just for experimenting and without using virtual functions. I was lead to believe that this is possible. What is my mistake? Or is it just not possible?

rubenvb
  • 74,642
  • 33
  • 187
  • 332
Lefteris
  • 3,196
  • 5
  • 31
  • 52
  • 3
    What are you **really** trying to do? You already have an inheritance scheme set up; why aren't you using `virtual` functions to get polymorphic behaviour instead of trying to hack something together with function pointers? – Karl Knechtel Dec 08 '10 at 13:14
  • 2
    possible duplicate of [C++ inheritance and member function pointers](http://stackoverflow.com/questions/60000/c-inheritance-and-member-function-pointers) – CB Bailey Dec 08 '10 at 13:17
  • I agree with what Karl Knechtel said. The `virtual` keyword already makes the compiler do all the hard work for you. – Mephane Dec 08 '10 at 13:25
  • To karl: I just don't want to use virtual functions as I stated in the question. I want to understand the inner workings of what is possible and what is not. To Charles: Yes the explanation there seems to be really good. – Lefteris Dec 08 '10 at 13:26

2 Answers2

5

A void (Parent::*)() takes an object of type Parent. The function Child::afunc requires an object of type Child. Converting the pointer to void (Parent::*)() would therefore allow for an invalid call.

Member function pointers can't really be used to implement polymorphism. The traditional way to implement polymorphism without using virtual is to use regular function pointers:

struct MyBaseClass
{
    void (*function)(MyBaseClass* this_pointer);
};
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • So it would work if `Child::afunc` was an override of a virtual function in the parent? – sje397 Dec 08 '10 at 13:19
  • Oh so by passing a pointer to the object of the class as an argument of every function for which we want to achieve polymorphism this can be implement eh? – Lefteris Dec 08 '10 at 13:30
2

It would be dangerous if that conversion worked as you would be able to call a function in class Child with a pointer or reference to a Parent which might not be of type Child.

If you are passing in a function and a pointer/reference you could use a boost::bind to achieve it.

CashCow
  • 30,981
  • 5
  • 61
  • 92