Meph
Meph

Reputation: 372

passing and casting method pointers

I struggle creating derived classes and passing method pointers from it to the base class, so that a function declared in the base class may call it (call the function of the derived class via interface).

The goal is to create derived classes to bring their own ressources and functions, but the call of a function declared there should be possible by calling one of them in the function the base class provides. For this i need to pass a member function pointer of the derived down to the base class.

Here's what I tried:

    class KeyFunction
{
    void(*KeyFunction::KeyFuncPtr)() = nullptr;   //pointer to a method of this class to store standard behavior for call

public:
    KeyFunction(void(*KeyFunction::KeyFunc)()) : KeyFuncPtr(KeyFunc)    {}                  //constructor which takes in standard behavior

    void func()       //standard call of the function
    {
        if(KeyFuncPtr)KeyFuncPtr(); //call with ensurance there's something to be called
    }

    void operator()() //make KeyFunction class callable
    {
        func();
    }
};

class customRessource{
public:
    string up = "UP";
    string down = "DOWN";

};

class customKeyFunc : public KeyFunction
{
    customRessource& r;
public:
    void moveup()               //possible behavior
    {
        cout << r.up;
    }
    void movedown()
    {
        cout << r.down;
    }

    customKeyFunc( void(*customKeyFunc::KeyFunc)() ) :KeyFunction( ( void(*KeyFunction::)() ) (KeyFunc) ){}


};

int main()
{
    customKeyFunc Up(&(customKeyFunc::moveup));   //setup functions
    customKeyFunc Down(&customKeyFunc::movedown);

    Up();                                         //call functions
    Down();

    getchar();
    return 0;
}

The main function at the end shows the supposed way to use the class .

First of all: my types in the constructors of each class go wild (i tried a lot of search about how to write member pointers right but i'm still not stable with the syntax) Can someone help me get them right ?

Can I even do this (especially casting down member pointers like i did in the customKeyFunc constructor)? Am I aproaching this the right way or do I think too complicated ?

Thank you in advance for your help !

Upvotes: 0

Views: 61

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69902

something like this?

#include <functional>
#include <string>
#include <iostream>

class customResource{
public:
    const std::string up = "UP";
    const std::string down = "DOWN";
};

class customKeyFunc
{
    const customResource& r;
public:
  customKeyFunc(const customResource& r) : r(r) {}

  void moveup()               //possible behavior
    {
        std::cout << r.up;
    }

    void movedown()
    {
        std::cout << r.down;
    }

};

int main()
{
  customResource r;
  customKeyFunc f(r);

  auto Up = std::function<void()>(std::bind(&customKeyFunc::moveup, f));
  auto Down = std::function<void()>(std::bind(&customKeyFunc::movedown, f));

  Up();                                         //call functions
  Down();

  return 0;
}

std::function<void()> is a polymorphic functor which will copy any object that:

  • is movable or copyable, and

  • implements void operator()

Upvotes: 1

Related Questions