相关疑难解决方法(0)

如何将函数存储到变量?

我认为他们被称为算子?(有一阵子了)

基本上,我想在变量中存储指向函数的指针,因此我可以从命令行指定我想要使用的函数.

所有函数都返回并采用相同的值.

unsigned int func_1 (unsigned int var1)
unsigned int func_2 (unsigned int var1)

function_pointer = either of the above?
Run Code Online (Sandbox Code Playgroud)

所以我可以通过以下方式调用它: function_pointer(my_variable)?

编辑:根据@ larsmans的建议,我得到了这个:Config.h:

class Config
{
public:
    unsigned static int (*current_hash_function)(unsigned int);
};
Run Code Online (Sandbox Code Playgroud)

Config.cpp:

#include "Config.h"
#include "hashes.h"
unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;
Run Code Online (Sandbox Code Playgroud)

hashes.h:

unsigned int kennys_hash(unsigned int out);
unsigned int kennys_hash_16(unsigned int out);
Run Code Online (Sandbox Code Playgroud)

hashes.cpp:

just implements the functions in the header
Run Code Online (Sandbox Code Playgroud)

main.cpp中:

#include "Config.h"
#include "hashes.h"
// in test_network:
    unsigned int hashed = Config::current_hash_function(output_binary);

//in …
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers

15
推荐指数
4
解决办法
3万
查看次数

标签 统计

c++ ×1

function-pointers ×1