C++: Get address from member function pointer - c++

I need am writing an operator== function that will need to compare two member function pointers and check to see if they point to the same function. I initially tried to do it like this:
typedef void(EventListenerClass::*EventHandler)(const Event&);
struct EventListener
{
EventListenerClass *_listener = NULL;
EventHandler _handler = NULL;
inline bool operator== (const EventListener& other) const
{ return addressof(_listener->*_handler) == addressof(other._listener->*_handler); };
};
But this gives me the following message:
Error: a pointer to a bound function may only be used to call the function
I found a potential solution here, which i will quote below:
In C++, pointer to member functions (PMFs) are implemented using a
wide pointer of sorts to handle all the possible call mechanisms; the
PMF needs to store information about how to adjust the ‘this’ pointer,
and if the function pointed to is virtual, where to find the vtable,
and where in the vtable to look for the member function. If you are
using PMFs in an inner loop, you should really reconsider that
decision. If that is not an option, you can extract the pointer to the
function that would be called for a given object/PMF pair and call it
directly inside the inner loop, to save a bit of time.
The syntax for this extension is
extern A a;
extern int (A::*fp)();
typedef int (*fptr)(A *);
fptr p = (fptr)(a.*fp);
I tried adapting this solution to my code as follows:
extern EventListenerClass a;
extern int (EventListenerClass::*fp)();
typedef int(*fptr)(EventListenerClass *);
fptr p = (fptr)(a.*fp);
But this just gave me the exact same error message as before. Can anyone tell me how i can compare these two member function pointers?

Related

Where are member functions stored for an object?

I'm experimenting with C++ to understand how class/structures and their respective objects are laid out in memory and I understood that each field of a class/structure is an offset into their respective object (so I can have a member variable pointer).
I don't understand why, even if I can have member function pointers, the following code doesn't work:
struct mystruct
{
void function()
{
cout << "hello world";
}
int c;
};
int main()
{
unsigned int offset_from_start_structure = (unsigned int)(&((mystruct*)0)->c);
unsigned int offset_from_start_structure2 = (unsigned int)(&((mystruct*)0)->function); // ERROR - error C2276: '&' : illegal operation on bound member function expression
return 0;
}
My question is: why does the line
unsigned int offset_from_start_structure = (unsigned int)(&((mystruct*)0)->c);
compile and returns me the offset of the "c" field from the start of the structure and the line
unsigned int offset_from_start_structure2 = (unsigned int)(&((mystruct*)0)->function);
doesn't even compile?
Member functions or pointers to them aren't stored in the object. (virtual functions are typically called through a pointer stored in a table to which an object has a single pointer to) This would be a huge waste of memory. They're typically stored in a code memory section, and are known to the compiler. The object (*this) is typically passed as an invisible parameter so the functions know on which object to operate when they are called.
So, in layman terms, you'd have
0x10001000 void A::foo
.......... {code for A::foo}
and
push a;
call A::foo (0x10001000)
pop a;
where a is the object you're calling foo on.
Member function pointers are in practice not stored in objects: there's no need. The C++ standard doesn't specify exactly how e.g. virtual functions are to be implemented, but the common practice for virtual member functions is that each object contains a pointer to a table of function pointers; this pointer is called a vtable pointer.
You might try to get hold of “Inside the C++ object model” by Stanley Lippman.
Or, you might just try to get hold of my old pointers tutorial, which was once referenced from Wikipedia's pointers article, before my then homepage site disappeared.
Regarding the second question, why taking the address of p->memberFunc makes the compiler choke a little, well that expression has no type, it's just a syntactical entity, which you can apply an argument list to in order to call the function.
To wit,
struct S
{
void foo() {}
};
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
S* p = 0;
typeid( &p->foo );
}
Compilation:
[W:\dev\test]
> g++ foo.cpp
foo.cpp: In function 'int main()':
foo.cpp:12:17: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say '&S::foo' [-fpermissive]
foo.cpp:12:22: warning: value computed is not used [-Wunused-value]
foo.cpp:12:22: warning: statement has no effect [-Wunused-value]
[W:\dev\test]
> _

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++ 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.

copying pointer to member functions from a structure

I have my code organized as following
class MyClass
{
public:
typedef void (MyClass::Ptr2func)();
}
struct abc
{
MyClass::Ptr2func ptr;
bool result;
}
void main()
{
MyClass myCls;
abc var;
//here is a lot of decision making code and pointer initializations
//I want to copy the pointer to function in another variable
MyClass::Ptr2func tempPtr;
tempPtr=var.ptr;
}
When I try to copy the var.ptr into tempPtr it gives me a compilation error that the argument list is missing. Also it gives me compilation error on myCls.*(var.ptr()); Is there a precedence issue? I have tried using parenthesis but nothing works. I hope someone can help me out on this.
Thanks and regards,
Saba Taseer
I believe that the problem is that your typedef
typedef void (MyClass::Ptr2func)();
Is not defining a typedef for a pointer to member function, but for a member function type. The typedef for a member function pointer would be
typedef void (MyClass::* Ptr2func)();
Notice the explicit pointer involved here. The type
typedef void (MyClass::Ptr2func)();
is actually a typedef for the type of a member function inside of MyClass that takes no arguments and returns void.
As for your final question, the proper syntax for calling a pointer to a member function is (I believe)
(myCls.*var.ptr)()
Notice that you must parenthesize the expression (myClass.*var.ptr) before trying to call it as a function, since the code
myCls.*(var.ptr())
Means "dereference the pointer-to-member returned by var.ptr() relative to object myCls.
Hope this helps!
This:
typedef void (MyClass::Ptr2func)();
defines a function type. The MyClass:: is completely extraneous in that definition; the final type is void (). (Why? You're in MyClass, so MyClass:: is treated as reinforcement that you want the typedef Ptr2func to belong to MyClass, not as a statement that you want the type to designate a member function.)
You want this:
typedef void (MyClass::*Ptr2func)();
which defines a pointer to a member function.
You're asking about compilation errors.
As I'm writing this your code does not have semicolong after each class definition.
Also, just about any other compiler than MSVC will react to void main; use standard int main.
As an utter beginner (making those kinds of mistakes) it's not a good idea to use member pointer.
Simply don't, but instead explain what you hoped to achieve by that.
Cheers & hth.

How do I call a member function pointer using a pointer to a constant object?

Here is an example of what I want to accomplish and how:
class MyClass
{
public:
void Dummy() const{}
};
typedef void (MyClass::*MemFunc)();
void (const MyClass * instance)
{
MemFunc func=&MyClass::Dummy;
// (instance->*func)(); //gives an error
(const_cast<MyClass *>instance->*func)(); // works
}
Why do compilers (gcc 3 & 4) insist that instance should be non-const? Would that const_cast cause issues?
FYI: instance` is not necessarily const, I just don't want a callee to mess with it.
What is happening here?
The error is in the line before. Change the typedef to
typedef void (MyClass::*MemFunc)() const;
To make it a pointer to a const member function type.
The difference might be more clear when considering this code and how it works:
typedef void FunctionType() const;
typedef FunctionType MyClass::*MemFunc;
A member-function pointer in particular is actually just a special case of a member-pointer in general. For a const member function, the function type of the member function is different than for a non-const member function. That is why the types have to match.
typedef void (MyClass::*MemFunc)();
Here you are defining a pointer to a function which might modify its object.
MemFunc func=&MyClass::Dummy;
Here you are assigning a function which won't change it's object to such a pointer. This is legal, since not changing is a subset of might change.
(instance->*func)();
Here you are trying to call a function which might change its object, using an object which cannot be changed.
You will need to change the definition of MemFunc