// include.cpp
#include "include.h"
void ObjectPointer::call( void (*func)() ) {
if ( this->myPointer != nullptr ) {
this->myPointer->func(); // <-- this is the problem area
}
}
This returns the compile error:
include.cpp:5: error: 'class Actions' has no member named 'func'
Well, thatâs correct, Actions doesnât contain âfuncâ but I thought I was defining a function pointer with void (*func)(), so then why is it being treated explicitly by the compiler? The expectation is to be able to write something like:
object->call( whoami );
Essentially to allow me to define a pointer to a specific behavior and make function calls. Now, SPECIFICALLY this isnât horribly important since it isnât required. Generally speaking Iâd just code the behaviors Iâm looking for, but I am interested as to why this doesnât work.
Yes, void (*func)() defines a function pointer. Itâs called âfuncâ, and itâs the name that the argument passed to call will be assigned to. Itâs most notably NOT a member of some class or struct. The form foo->func is a request for the member âfuncâ of the object âfooâ, which the compiler rightfully objects to. The proper syntax to invoke func is just âfunc()â.
With that in mind, itâs clear that you need to pass a function pointer - not the name of a member - to call. In most OO languages, this would look like object->call(object->mypointer->whoami), but this is C++, so maybe not.
It looks like youâre trying to look up the member of the Actions class by name in the function call. I have no idea how you would do that in C++.
If the idea is to have objects that identify their class in some way, Iâd say passing around function pointers is the wrong solution. But without some clear idea of the ultimate goal, itâs hard to say for sure, much less what a better solution would be.
Part of your problem is that func has a function signature of void (void) - it doesnât accept any arguments and doesnât return anything.
I think from the looks of things youâre trying to pass an Actions * to func in which case func has to be defined as void (*func)(Actions *).
You would then call it as:
if ( this->myPointer != nullptr && func != nullptr) { // Make sure func isn't a nullptr either
func(this->myPointer);
}
Now that thatâs all said, Iâll warn you that if your intention was to pass whoami as a function pointer then itâs not that simple.
If youâre trying to call a member function (e.g. Actions::whoami) with a function pointer, thatâs a little more difficult for good reasons.
Really though Iâm not completely sure what your end goal is, at a guess it looks like youâre weighing up the benefits of using a function pointer vs using a virtual function on a class.
Yeah, this is kind of what I was thinking. whoami has no explicit scope, and itâs not implied anywhere. This was mostly just a test I wrote up based on some function pointer examples I was reading online to see if it were possible to do it this way. I think it probably is possible to do this, but I think you are right: this would be quite difficult and I donât think this code would have (not that it compiles anyways) the intended effect even if it did.