简体   繁体   English

创建指向成员函数的指针数组

[英]Creating an Array of pointers to member functions

In short, I tried searching on how to do this, but I seem to be missing something.简而言之,我尝试搜索如何执行此操作,但我似乎遗漏了一些东西。 One constraint to my problem: Human.h cannot change.我的问题的一个限制条件是:Human.h 无法更改。 We must operate with what we've been given.我们必须根据所获得的进行操作。 I am also told to create the array of pointers to members to decide on which function needs to be called.我还被告知创建指向成员的指针数组,以决定需要调用哪个 function。

Here's what I have:这是我所拥有的:

Human.h人类.h

class Human
{
private:
    void meleeAttack(std::string const& target);
    void rangedAttack(std::string const& target);
    void intimidatingShout(std::string const& target);
public:
    void action(std::string const& action_name, std::string const& target);
};

Human.cpp人类.cpp

#include "Human.h"

typedef void (Human::* Human_mem_fnPtr)(std::string target);

void Human::meleeAttack(std::string const& target)
{
    std::cout << "Melee Attack performed on " << target << "!\n";
}

void Human::rangedAttack(std::string const& target)
{
    std::cout << "Ranged Attack performed on " << target << "!\n";
}

void Human::intimidatingShout(std::string const& target)
{
    std::cout << "Shout performed on " << target << "!\n";
}

void Human::action(std::string const& action_name, std::string const& target)
{
    //error on initialization--expression must be an lvalue or function designation--but they ARE func designations...
    Human_mem_fnPtr fnPtr[] = {&Human::meleeAttack(target), &Human::rangedAttack(target), &Human::intimidatingShout(target)}; 
}

From what I found online, I am going in the right direction here.从我在网上找到的内容来看,我正在朝着正确的方向前进。 What am I missing?我错过了什么?

A couple of points:几点:

  • Doing pointers to functions is made much easier with the std::function<> template class.使用std::function<>模板 class 使指向函数的指针变得更加容易。
  • Using a old-style array is not really the best choice these days.现在使用旧式数组并不是最好的选择。

A map or unordered_map would be a much better option, with a definition like this: mapunordered_map将是更好的选择,其定义如下:

using ActionMap = std::unordered_map<const std::string, std::function<void(const std::string&)>;

When adding your functions to this map you would use something like the following:将您的函数添加到此 map 时,您将使用如下内容:

mActionMap["rangedAttack"] =  std::mem_fn(&Human::rangedAttack);

This will give you a cleaner and easier to maintain option and should compile cleanly.这将为您提供一个更干净、更易于维护的选项,并且应该可以干净地编译。

Note that the std::mem_fn is required to wrap a member function of a class.请注意,需要std::mem_fn来包装 class 的成员 function。

Edit: Per your comment below, Id still suggest using as many of the modern C++ constructs as possible.编辑:根据您在下面的评论,我仍然建议尽可能多地使用现代 C++ 结构。

using ActionFunc = std::function<void(const std::string&)>;

And then:然后:

ActionFunc actions[] = { std::mem_fn(&Human::rangedAttack), ...}

or:或者:

std::array<ActionFunc> actions =...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM