C++ 如何将函数存储到变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5582869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I store a function to a variable?
提问by NullVoxPopuli
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?
so then I could call it by going: function_pointer(my_variable)?
那么我可以通过以下方式调用它: function_pointer(my_variable)?
EDIT: as per @larsmans's suggestion, I've gotten this: Config.h:
编辑:根据@larsmans 的建议,我得到了这个:Config.h:
class Config
{
public:
unsigned static int (*current_hash_function)(unsigned int);
};
Config.cpp:
配置.cpp:
#include "Config.h"
#include "hashes.h"
unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;
hashes.h:
哈希值.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:
主.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;
}
the error I get:
我得到的错误:
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
回答by Jon
The simplest you can do is
你能做的最简单的是
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.
这是一个裸函数指针,不能用于指向除自由函数以外的任何内容。
You can make it less painful if your compiler supports the C++0x auto
keyword:
如果您的编译器支持 C++0xauto
关键字,您可以减少痛苦:
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:
还有许多其他选项可以提供通用性,例如:
- You can use
boost::function
with any C++ compiler - With a compiler implementing features of C++0x you can use
std::function
- 您可以
boost::function
与任何 C++ 编译器一起使用 - 使用实现 C++0x 功能的编译器,您可以使用
std::function
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).
这些可以用来指向任何可以用适当的签名调用的实体(它实际上是实现了operator()
被称为函子的对象)。
Update (to address updated question)
更新(解决更新的问题)
Your immediate problem is that you attempt to use Config::current_hash_function
(which you declare just fine) but fail to define it.
您的直接问题是您尝试使用Config::current_hash_function
(您声明很好)但未能定义它。
This defines a global static pointer to a function, unrelated to anything in class Config
:
这定义了一个指向函数的全局静态指针,与 中的任何内容无关class Config
:
unsigned static int (*current_hash_function)(unsigned int) = kennys_hash_16;
This is what you need instead:
这是你需要的:
unsigned int (*Config::current_hash_function)(unsigned int) = kennys_hash_16;
回答by Fred Foo
No, these are called function pointers.
不,这些被称为函数指针。
unsigned int (*fp)(unsigned int) = func_1;
回答by mkaes
You could also use function either from the c++0x or from boost. That would be
您还可以使用来自 c++0x 或 boost 的函数。那将是
boost::function<int(int)>
and then use bind to bind your function to this type.
然后使用 bind 将您的函数绑定到此类型。
Ok here would be a example. I hope that helps.
好的,这将是一个例子。我希望这有帮助。
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);
}
回答by 0x476f72616e
From C++11 you can use std::function
to store functions. To store function you use it as follsonig:
从 C++11 开始,您可以使用std::function
来存储函数。要存储功能,您可以将其用作 follsonig:
std::function<return type(parameter type(s))>
std::function<返回类型(参数类型)>
as an example here it is:
作为一个例子,它是:
#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);
}
回答by Naszta
typedef unsigned int (*PGNSI)(unsigned int);
PGNSI variable1 = func_1;
PGNSI variable2 = func_2;
回答by Alexander Gessler
unsigned int (* myFuncPointer)(unsigned int) = &func_1;
However, the syntax for function pointers is awful, so it's common to typedef
them:
然而,函数指针的语法很糟糕,所以它们很常见typedef
:
typedef unsigned int (* myFuncPointerType)(unsigned int);
myFuncPointerType fp = &func_1;
回答by yasouser
IF you have Boostinstalled, you can also check out Boost Function.
如果您安装了Boost,您还可以查看Boost Function。