0

I am working on a piece of core code where a function will be stored within an object and then will be called later when needed. So far I have used function pointers to build a first draft of this:

class caller {
int (*f)();
caller(int (*function)()){
f = function;
}
void timeIsRight(){
f();
}

caller(otherFunction);

This works for calling functions from other areas of the code however I would like to be able to handle passing object members to the caller I.E:

caller(object.aFunction);

Although my compiler is complaining about the above which leads me to believe I am approaching this wrong (I have tried a few other ways of writing the above to no avail.

I have tried moving the caller into the object class:

class object{
...
int myMember(){...}
caller(myMember);
...
}

Although I get the error int(namespace::object::*)() is incompatible with int(*)() This would tell me that I need to tell the caller where the functions originates.

Now ideally I would like caller to be able to take any function that returns an int and has no parameters irregardless of where it is located (namespace, class ect)

From my reading so far two possible routes I could go down are:

  • Creating Templates
  • Use Member Pointers

I am also willing to switch from functions pointers to function objects or lamdas if these would fulfill my needs.

Would any of the above approaches be able to get me the result I want and if so I would greatly appropriate a push in the right direction, as I have found a lot of breadth to my search with little helpful depth.

Thanks all.

Please note I am after a class/namespace agnostic approach, which is my primary design constraint. If these are not possible with function pointers I am happy to move to objects, lamdas to building something else up.

However as I do not know the class at compile time the linked answer will not assist with my problem.

  • 2
    You can't just call a method of another class, using an unadorned function pointer. Class methods and functions are different in some fundamental ways. A pointer to a class method, and a pointer to a function, are different types. For starters, when you have a pointer to a class method, you will also need a pointer or a reference to an instance of the class whose method you wish to invoke. See the linked question for more information. – Sam Varshavchik Jul 12 '18 at 20:29
  • 1
    Perhaps using some form of std::function will help here. – meat Jul 12 '18 at 20:30
  • @SamVarshavchik I have tired to read over the linked question however this seem to create class specific calls as opposed to the class agnostic solution I want to develop. If pointers are not able of this agnostic call then I am happy to move to another method that is. Apologies if I have misunderstood the attached answer – user10052900 Jul 12 '18 at 20:34
  • @meat Do you know if there is a good chance these could help with the class-agnostic approach for these function calls? And if so could you give me a push in the right direction? – user10052900 Jul 12 '18 at 20:35
  • "Class agnostic" is not possible in C++. C++ does not work this way. Strong typing is a core fundamental aspect of C++. – Sam Varshavchik Jul 12 '18 at 20:47
  • std::function and std::bind might help. You may also consider the c++17 syntax template for optimizing member function pointer size. – Red.Wave Jul 13 '18 at 07:26
  • @Red.Wave Overnight I was thinking about creating "base" class with a virtual function inside. Then having each object that I need to push inherit from this base object and overwriting the virtual function. Then could I store `int(base::virFunc)()` and call the function that is overwritten by the derived class. Is that possible? / Is this bad form? – user10052900 Jul 13 '18 at 10:58

0 Answers0