如何将函数存储到变量?

Nul*_*uli 15 c++ function-pointers

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

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

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

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 main():
        else if (strcmp(argv[i], "-kennys_hash_16") == 0)
        {
            Config::current_hash_function = kennys_hash_16;
        }
        else if (strcmp(argv[i], "-kennys_hash_8") == 0)
        {
            Config::current_hash_function = kennys_hash;
        }
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

g++ -o hPif src/main.o src/fann_utils.o src/hashes.o src/Config.o -lfann -L/usr/local/lib 
Undefined symbols:
  "Config::current_hash_function", referenced from:
      test_network()     in main.o // the place in the code I've selected to show
      auto_test_network_with_random_data(unsigned int, unsigned int, unsigned int)in main.o
      generate_data(unsigned int, unsigned int, unsigned int)in main.o
      _main in main.o // the place in the code I've selected to show
      _main in main.o // the place in the code I've selected to show
      generate_train_file()     in fann_utils.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [hPif] Error 1
Run Code Online (Sandbox Code Playgroud)

Jon*_*Jon 18

你能做的最简单的事情就是

unsigned int (*pFunc)(unsigned int) = func_1;
Run Code Online (Sandbox Code Playgroud)

这是一个裸函数指针,不能用于指向除自由函数之外的任何东西.

如果您的编译器支持C++ 0x auto关键字,您可以减少痛苦:

auto pFunc = func_1;
Run Code Online (Sandbox Code Playgroud)

无论如何,您可以使用调用该函数

unsigned int result = pFunc(100);
Run Code Online (Sandbox Code Playgroud)

还有许多其他选项可以提供通用性,例如:

  • 您可以使用boost::function任何C++编译器
  • 使用编译器实现C++ 0x的功能,您可以使用 std::function

这些可以用于指向可以使用适当的签名调用的任何实体(它实际上是实现operator()称为仿函数的对象).

更新(以解决更新的问题)

您当前的问题是您尝试使用Config::current_hash_function(您声明正常)但未能定义它.

这定义了一个函数的全局静态指针,与以下任何内容无关class Config:

unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;
Run Code Online (Sandbox Code Playgroud)

这就是你需要的:

unsigned int (*Config::current_hash_function)(unsigned int) = kennys_hash_16;
Run Code Online (Sandbox Code Playgroud)


Fre*_*Foo 5

不,这些被称为函数指针.

unsigned int (*fp)(unsigned int) = func_1;
Run Code Online (Sandbox Code Playgroud)

  • @Michael:你可以用`fp(arg)`来调用,因为函数指针是一个普通的函数.试试看. (3认同)

mka*_*aes 5

您也可以使用c ++ 0x或boost中的函数.那就是

boost::function<int(int)>
Run Code Online (Sandbox Code Playgroud)

然后使用bind将您的函数绑定到此类型.

看看这里这里

好的,这是一个例子.我希望有所帮助.

int MyFunc1(int i)
{
    std::cout << "MyFunc1: " << i << std::endl;
    return i;
}

int MyFunc2(int i)
{
    std::cout << "MyFunc2: " << i << std::endl;
    return i;
}

int main(int /*argc*/, char** /*argv*/)
{
    typedef boost::function<int(int)> Function_t;

    Function_t myFunc1 = boost::bind(&MyFunc1, _1);
    Function_t myFunc2 = boost::bind(&MyFunc2, _1);

    myFunc1(5);
    myFunc2(6);
}
Run Code Online (Sandbox Code Playgroud)


0x4*_*16e 5

从 C++11 开始,您可以std::function用来存储函数。要存储功能,您可以将其用作 follsonig:

std::function<返回类型参数类型)>

作为一个例子,它是:

#include <functional>
#include <iostream>

int fact (int a) {
    return a > 1 ? fact (a - 1) * n : 1;
}

int pow (int b, int p) {
    return p > 1 ? pow (b, p - 1) * b : b;
}

int main (void) {
    std::function<int(int)> factorial = fact;
    std::function<int(int, int)> power = pow;

    // usage
    factorial (5);
    power (2, 5);
}
Run Code Online (Sandbox Code Playgroud)