如何在C ++中重新分配函数名称 [英] How do I re-assign a function name in C++

查看:108
本文介绍了如何在C ++中重新分配函数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中有一个与此示例问题类似的问题。在这种情况下,我有两个具有相同接口的成员函数。根据传递给超级函数的字符串中的信息,我想将两个成员函数之一分配给变量 class_func 。有办法吗?

I have a problem in C++ that is similar to this example problem. In this case I have two member-functions that have an identical interface. Based on the information in a string passed to the super function, I would like to assign one of the two member functions to the variable class_func. Is there a way to do this?

// test.hpp
class TestClass
{
public:
    double master_function(double a, double b, std::string func_name);
private:
    double add(double a, double b);
    double subtract(double a, double b);
};

// test.cpp
double TestClass::master_function(double a, double b, std::string func_name)
{
    if (func_name == std::string("Add") const auto& class_func = add;
    else const auto& class_func = subtract;
    return class_func(a, b);
}
// =========================================================================

double TestClass::add(double a, double b)
{
    return a + b;
}
// =========================================================================

double TestClass::subtract(double a, double b)
{
    return a - b;
}

换句话说,我试图分配成员函数 add 命名为 class_func ,因此 master_function if 语句下面的代码c $ c>可以是统一的,无论用户要使用哪个函数,在下面显示的形式中,我都会得到错误必须引用非静态成员函数,但是我不确定这意味着什么或如何解决。另外,我使用的是C ++ 17编译器,因此,如果有一种最适合C ++ 17的现代方法,我会对学习它感兴趣。

In other words, I am trying to assign the member-function add or subtract to the name class_func, so the code underneath the if statement in master_function can be uniform regardless of which function the user wants to use. In the form shown below I get the error Reference to non-static member function must be called out, but I am not totally sure what this means or how to fix it. In addition, I am using a C++17 compiler, so if there is a modern approach that works best with C++17 I would be interested in learning it.

推荐答案

您要查找的术语是成员函数指针,但是我们可以在不显式指定该类型的情况下进行操作。代码的问题不仅在于您尝试引用成员函数(即& TestClass :: add )的方式,还在于您创建了这些成员函数嵌套范围中的别名(在 if / else 下),这意味着它们在<$ c中不可见$ c> return 语句。

The term you are looking for is member function pointer, but we can do without explicitly specifying that type. The problem with your code is not only in the way you try to refer to a member function (that would be &TestClass::add), but also that you create those aliases in a nested scope (under if/else), meaning they won't be visible in the return statement.

最简单的更改是:

auto class_func = &TestClass::add; // pick one default

if (func_name == "Subtract")
{
    class_func = &TestClass::subtract;
}
else
{
    assert(func_name == "Add"); // optional
}

return class_func(a, b);

之所以有效,是因为添加函数具有完全相同的类型:

This works because the add and subtract functions have the exact same type:

double (TestClass::*)(double a, double b)

但是是的,为什么那些函数不是静态?它们不适用于课程实例。将它们设置为 static ,以上操作仍然有效,只需注意 class_fun 的类型将是一个简单的函数指针:

But yeah, why are those functions not static? They do not work with a class' instance. Make them static and the above will still work, just note that the type of class_fun will be a simple function pointer:

double (*)(double a, double b)






现在,您知道类型了,就可以在不改变其中一个功能优先于另一个功能的情况下进行更改。代码:


Now that you know the types, you could change this in a way that does not privilege one function before the other in the code:

decltype(&TestClass::add) class_func = nullptr;

if (func_name == "Add")
{
    class_func = &TestClass::add;
}
else if (func_name == "Subtract")
{
    class_func = &TestClass::subtract;
}

assert(class_func != nullptr);
return class_func(a, b);

如评论中所述,如 if - else 链开始变长,在字符串和函数指针之间使用(哈希)映射变得越来越有意义。

As mentioned in the comments, as that if-else chain starts to get longer, it makes more and more sense to use a (hash)map between strings and function pointers.

这篇关于如何在C ++中重新分配函数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆