【问题标题】:passing and casting method pointers传递和转换方法指针
【发布时间】:2016-05-15 13:09:46
【问题描述】:

我很难创建派生类并将方法指针从它传递给基类,以便在基类中声明的函数可以调用它(通过接口调用派生类的函数)。

我们的目标是创建派生类来带来它们自己的资源和函数,但是通过在基类提供的函数中调用其中一个函数来调用声明的函数应该是可能的。为此,我需要将派生的成员函数指针向下传递给基类。

这是我尝试过的:

    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;
}

最后的 main 函数显示了使用该类的假定方式。

首先:我在每个类的构造函数中的类型变得疯狂(我尝试了很多关于如何正确编写成员指针的搜索,但我的语法仍然不稳定) 有人可以帮我把它们弄好吗?

我什至可以这样做(尤其是像我在 customKeyFunc 构造函数中那样向下转换成员指针)?我是以正确的方式接近这个还是我认为太复杂了?

提前感谢您的帮助!

【问题讨论】:

  • 在我看来您正在尝试重新发明虚拟方法。重新发明轮子始终是一项艰巨的工作。
  • 似乎是CRTP的用例
  • 不使用虚函数有什么原因吗?
  • 因为我不想...问题是我需要为每个函数创建一个新类(继承基),即使这些函数在相同的资源上工作,如果我这样做了虚函数
  • 好的,但是您编写的任何内容都没有排除使用虚拟方法。

标签: c++ derived-class base-class member-function-pointers member-functions


【解决方案1】:

这样的?

#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&lt;void()&gt; 是一个多态函子,它将复制任何对象:

  • 可移动或可复制,并且

  • 实现void operator()

【讨论】:

  • 是的,这很好用!太棒了,谢谢;我现在可以用这个函数 类定义一个数组,这基本上就是我想要创建的样子。
  • 对于协议:问题本身的实际解决方案可能可以在关于 std::functions 的 std 文档中进行研究(如果有人真的想自己做的话)
  • @Meph - 是的,您可以创建一个 std::functions 数组。是的,答案在文档中,如果你想进步,你应该阅读它......
猜你喜欢
  • 1970-01-01
  • 2013-01-13
  • 2015-10-07
  • 1970-01-01
  • 2011-01-19
  • 2013-04-03
  • 1970-01-01
  • 2016-05-14
  • 2015-02-11
相关资源
最近更新 更多