如何将函数存储到变量? [英] How do I store a function to a variable?

查看:115
本文介绍了如何将函数存储到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为它们被称为函子? (已经有一段时间了)

I think they are called functors? (it's been a while)

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

Basically, I want to store a pointer to a function in a variable, so I can specify what function I want to use from the command line.

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

all the functions return and take the same values.

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

function_pointer = either of the above?

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

so then I could call it by going: function_pointer(my_variable)?

编辑:
按照@larsmans的建议,我得到了:
Config.h:

as per @larsmans's suggestion, I've gotten this: Config.h:

class Config
{
public:
    unsigned static int (*current_hash_function)(unsigned int);
};

Config.cpp:

Config.cpp:

#include "Config.h"
#include "hashes.h"
unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;

哈希.h:

unsigned int kennys_hash(unsigned int out);
unsigned int kennys_hash_16(unsigned int out);

hashes.cpp:

hashes.cpp:

just implements the functions in the header

main.cpp中的函数:

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

我得到的错误:

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


推荐答案

最简单的方法是

unsigned int (*pFunc)(unsigned int) = func_1;

这是一个裸函数指针,除自由函数外,不能用于指向其他任何东西。

This is a bare function pointer, which cannot be used to point to anything other than a free function.

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

You can make it less painful if your compiler supports the C++0x auto keyword:

auto pFunc = func_1;

在任何情况下,都可以使用

In any case, you can call the function with

unsigned int result = pFunc(100);

还有许多提供通用性的选项,例如:

There are many other options that provide generality, for example:


  • 您可以将 boost :: function 与任何C ++编译器一起使用

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

  • You can use boost::function with any C++ compiler
  • With a compiler implementing features of C++0x you can use std::function

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

These can be used to point to any entity that can be invoked with the appropriate signature (it's actually objects that implement an operator() that are called functors).

您的直接问题是您尝试使用 Config :: current_hash_function (您声明还不错),但未能定义它。

Your immediate problem is that you attempt to use Config::current_hash_function (which you declare just fine) but fail to define it.

此函数定义了指向函数的全局静态指针,与 class Config 中的任何内容:

This defines a global static pointer to a function, unrelated to anything in class Config:

unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;

这是您需要的:

unsigned int (*Config::current_hash_function)(unsigned int) = kennys_hash_16;

这篇关于如何将函数存储到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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