it might be strange what i'm trying to see and i will try to clarify as much as possible. I'm using gcc 4.8 on ubuntu 14.04 and C++11.
What i want trying to do is:
- make a class A
- make a function in that class A which gets as argument
- a pointer to a class member of the same class
- make a new class B which inherits from A
- make a new method of class B
give a pointer to that method of class B to a method of parentclass A as argument
class A{ typedef void(A::*METHOD); void executeMethod(METHOD arg){}; } class B : A{ void sampleMethod(){}; void childMethod(){ this->executeMethod(&B::sampleMethod); //<== error } }
However this brings me the following error in codeblocks:
error: no matching function to call for 'B::executeMethod(void B::*)'
Is there any way around this? Is there anything else i need to do to make it clear to you, what i'm trying to accomplish?
defines
METHOD
to be a pointer of typevoid*
to a member variable ofA
, not a pointer to a member function ofA
.You need:
Even with that change, you can't use a member function of
B
to pass as an argument whereMETHOD
is expected.You can make the function that you want to pass a virtual member function of
A
and use it.The problem is that
sampleMethod
isn't a member ofA
, it's a member ofB
and can't convert to avoid(A::*)
.Did you consider using virtual methods perhaps?
You cannot directly call child method from a base class, but you can use template:
But more flexible solution would be to use
std::function
andstd::bind
as then you can pass methods which signature does not match.To make a callable object bound to a member function you should use std::bind. This will create a forwarding wrapper which will call sampleMethod with some of its parameters pre specified. In this case, the "this" parameter will be bound.
std::bind(&B::sampleMethod, this);