Providing an instanceable class's method as a function pointer? - c++

Here is what I'd like to do. I have a function pointer which wants a function like this:
void func(int a);
so I have a class:
class Foo {
public:
void func(int a);
};
Foo *foo = new Foo;
something->setFunction(foo->func);
or in my case:
testWidget[count] = new TestWidget;
testWidget[count]->eventMouseClick.addHandler(testWidget[0]->silly);
But this gives me:
Error 5 error C3867:
'TestWidget::silly': function call
missing argument list; use
'&TestWidget::silly' to create a
pointer to
member c:\users\josh\documents\visual
studio
2008\projects\agui\alleg_5\main.cpp 190
Is there a way I could make this work without using a static function?
Thanks

Is there a way I could make this work without using a static function?
No. You can't convert a pointer to member function to an ordinary function pointer.
If the callback accepted any callable object (or a std::function, for example), then you could bind the object to the member function and pass the result of that; unfortunately, you can't convert that result to an ordinary function pointer, though.

Your question is not very clear to me. But the error message from the compiler makes me feel that probably you want something like this something->setFunction(&foo->func);
An example might help in case of overloads
struct T{
void func(int){}
void func(double){}
};
void f(void (T::*f)(int)){}
void f(void (T::*f)(double)){}
int main(){
void (T::*fi)(int) = &T::func;
void (T::*fd)(double) = &T::func;
f(fi);
f(fd);
}

Related

Unknown Syntax void (Type::m_function)()

I am currently reading through some code and came across a few lines I do not understand.
First
void Foo()
{
(((Type*)parent)->*m_function)();
}
As far as I can tell they are casting the parent to Type and then calling a dereferenced function? I am not sure I have seen the ->*m_function before.
Also I can not see where m_function is declared perhaps here? Which contains more syntax I do not understand. Is it declaring a function that returns void and takes a parameter of a function? But where is the function name?
class Foo()
{
void (Type::*m_function)();
};
It's calling a member function using a pointer to this function: C++ Call Pointer To Member Function
Yes, void (Type::*m_function)(); declares m_function member of type "member function of Type taking 0 args and returning void"

Function pointer as parameter in a class method declaration

I'm trying to make a class method that takes a function pointer (regular C function pointer, not a class method pointer) as a parameter. The only thing that comes up when I search is how to create/use a member function pointer, which I'm NOT trying to do. So here's a method that takes a function pointer that returns a bool and takes two ints as parameters:
class ExampleA
{
public:
void sort(WHAT_GOES_HERE); // Should it be the same as method definition below?
};
ExampleA::sort(bool (*customSort)(int, int)) // Hopefully this is correct
{
// Use function pointer
}
Is there a way to declare the parameter in the method declaration without naming it like a method with an int parameter?
class ExampleB
{
public:
void someFunction(int); // Not named here
};
ExampleB::someFunction(int varName)
{
// do whatever
}
Yep! Just leave out the name.
void sort(bool (*)(int, int));
bool (*)(int, int)
Basically, remove the name of the variable to get a declaration without a variable name.
However, you are often better off with a typedef:
typedef bool(*custom_sorter)(int, int);
class ExampleA {
public:
void sort(custom_sorter);
};
ExampleA::sort(custom_sorter customSort) {
// Use function pointer
}
which is equivalent.
As I personally hate the syntax to declare a function pointer, in C++11 I might do:
template<class T> using type=T;
...
void sort(type<bool(int,int)>*)
which puts the signature type together, then I put a * after it to make it a pointer.
But I'm strange.
The declaration should match the definition, so WHAT_GOES_HERE should be bool (*customSort)(int, int), and also the function definition should have the return type void specified.
You can optionally leave out the name customSort, it makes no difference.
This is somewhat inflexible; consider making it a function template that accepts a functor or a std::function instead of a function pointer; then your callers can call it with a wider range of functions.

C++ Passing a pointer to a member function as an argument

I need to pass a pointer to a member function as an argument to a member function in another class. What I'm doing is something like below.
I want to call int Processor::AddJob(void (_pFunc)(void*), void* _pArg) method with void* ProcessHandler::Task(void* a) as the first argument.
I did it as
TaskFunc pFunc1 = &ProcessHandler::Task;
p_Processor->AddJob(pFunc1,10);
But it gives the error
error: no matching function for call to Processor::AddJob(void*
(ProcessHandler::&)(void), int)’ Processor.h:47: note: candidates
are: int Processor::AddJob(void* ()(void), void*)
Can someone please help me on this.My implementation is as follows.(Not the exact code-it is much larger than this)
class Processor
{
public:
Processor();
virtual ~Processor();
int AddJob(void *(*_pFunc)(void*), void* _pArg);
};
int Processor::AddJob(void *(*_pFunc)(void*), void* _pArg)
{
//Job addition related code goes here
}
/////////////////////////////////////////////////////////////////////////////
typedef void* (ProcessHandler::*TaskFunc)(void*);
class ProcessHandler
{
public:
ProcessHandler(Processor* _pProcessor);
virtual ~ProcessHandler();
void* Task(void* a);
void Init();
private:
Processor* p_Processor;
};
void* ProcessHandler::Task(void* a)
{
//Task related code goes here
}
void ProcessHandler::Init()
{
TaskFunc pFunc1 = &ProcessHandler::Task;
p_Processor->AddJob(pFunc1,10); //This give the compile error.
}
/////////////////////////////////////////////////////////////////////////
int main()
{
Processor* pProcessor = new Processor();
ProcessHandler* pProcessHandler = new ProcessHandler(pProcessor);
pProcessHandler->Init();
}
You can only pass static member functions via the address-of operator. Regular member functions don't work. You should consider using std::function.
In C++ I would suggest to do_NOT use pointers to functions(raw pointers are arguable too).
You should use std::function(boost::function) for more generality, or
template. Latter gives you a bit perfomance, but less typecheck.
void (_pFunc)(void*) is a C-Function pointer, and as such, you can only either pass C-Functions via that pointer, or static functions.
void (ProcessHandler::*)(void); //Since you already have a Typedef, pass that as 'type'
should help you call a member function.
Call it with the pointer to member function calling syntax:
(objectPointer->*functionPointer)();

C++: Function pointer to another class function

I have 2 classes
class B {
public:
int func(int i);
};
class A {
public:
typedef int (B::*fPtr)(int);
void run();
B* mB;
};
void A::run() {
// create a pointer
fPtr p = &(B::func);
// invoke the function
mB->*p(2); <------- Compilation Error
}
What i need is to create a pointer to func() in A's run function. I get a compilation error saying that mB is not corresponding to a function with 1 argument.
please help
You need to put parentheses around the function expression:
(mB->*p)(2);
But as others have pointed out, there's almost certainly a better way to do what you're trying to do.
Instance methods on a class always have a hidden first parameter for the this pointer, thus it is incompatible with your function pointer typedef. There is no way directly to obtain a pointer to a member function. The typical workaround is to use a "thunk" where you pass a static function that accepts a generic "catch all" parameter (such as void *) which can be statically cast to a pointer of your choosing on which you can invoke the member function. Example:
class B
{
public:
static void MyThunk(void * obj)
{
static_cast<B *>(obj)->MyRealFunc();
}
void MyRealFunc()
{
// do something here
}
// . . .
};
You can get a pointer to the static function easily as it has no 'hidden this', just reference it using B::MyThunk. If your function requires additional parameters, you can use something like a functor to capture the necesssary parameters and state.
You should definitely read this C++ FAQ Lite page which tells you much more about all this: Pointers to member functions
why can you not call mB->func(2);?
If you need different functions for B perhaps look into virtual functions and class inheritance

c++ member function pointer problem

I'm new to c++ . I want to know about object pointer and pointer to member function . I wrote a code which is following:
code :
#include <iostream>
using namespace std;
class golu
{
int i;
public:
void man()
{
cout<<"\ntry to learn \n";
}
};
int main()
{
golu m, *n;
void golu:: *t =&golu::man(); //making pointer to member function
n=&m;//confused is it object pointer
n->*t();
}
but when i compile it it shows me two error which is following:
pcc.cpp: In function ‘int main()’:
pcc.cpp:15: error: cannot declare pointer to ‘void’ member
pcc.cpp:15: error: cannot call member function ‘void golu::man()’ without object
pcc.cpp:18: error: ‘t’ cannot be used as a function.
my question are following :
What I'm doing wrong in this code ?
How to make object pointer ?
How to make pointer to member function of a class and how to use them ?
Please explain me these concept.
Two errors corrected here:
int main()
{
golu m, *n;
void (golu::*t)() =&golu::man;
n=&m;
(n->*t)();
}
you want a pointer to function
the priority of the operators is not the one you expected, I had to add parenthesis. n->*t(); is interpreted as (n->*(t())) while you want (n->*t)();
A member function pointer has the following form:
R (C::*Name)(Args...)
Where R is the return type, C is the class type and Args... are any possible parameters to the function (or none).
With that knowledge, your pointer should look like this:
void (golu::*t)() = &golu::man;
Note the missing () after the member function. That would try to call the member function pointer you just got and thats not possible without an object.
Now, that gets much more readable with a simple typedef:
typedef void (golu::*golu_memfun)();
golu_memfun t = &golu::man;
Finally, you don't need a pointer to an object to use member functions, but you need parenthesis:
golu m;
typedef void (golu::*golu_memfun)();
golu_memfun t = &golu::man;
(m.*t)();
The parenthesis are important because the () operator (function call) has a higher priority (also called precedence) than the .* (and ->*) operator.
'void golu:: *t =&golu::man();' should be changed to 'void (golu:: *t)() =&golu::man;' you are trying to use pointer to function not pointer to result of a static function!
(1) Function pointer is not declared properly.
(2) You should declare like this:
void (golu::*t) () = &golu::man;
(3) Member function pointer should be used with object of the class.