Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用从另一个函数c++;_C++ - Fatal编程技术网

C++ 调用从另一个函数c++;

C++ 调用从另一个函数c++;,c++,C++,我想知道,如何在我定义函数的地方生成函数。然后我可以调用定义的函数。让我举个例子试试 void funcToCall() { std::cout<<"Hello World"<<std::endl; } void setFuncToCall(void func) { //Define some variable with func } void testFuncCall() { //Call function that has been def

我想知道,如何在我定义函数的地方生成函数。然后我可以调用定义的函数。让我举个例子试试

void funcToCall() {
    std::cout<<"Hello World"<<std::endl;
}

void setFuncToCall(void func) {
    //Define some variable with func
}

void testFuncCall() {
    //Call function that has been defined in the setFuncToCall
}

setFuncToCall(funcToCall()); //Set function to call
testFuncCall(); //Call the function that has been defined
void funcToCall(){

std::cout您需要一个函数指针。如果您先使用函数指针,那么使用函数指针就更容易了

typedef void (*FuncToCallType)();

FuncToCallType globalFunction; // a variable that points to a function

void setFuncToCall(FuncToCallType func) {
    globalFunction = func;
}

void testFuncCall() {
    globalFunction();
}

setFuncToCall( funcToCall ); //Set function to call,NOTE: no parentheses - just the name 
testFuncCall(); //Call the function that has been defined
正如其他答案所建议的那样,您也可以使用函数之类的对象。但这需要运算符重载(即使它仍然对您隐藏),并且通常与模板一起使用。
它提供了更大的灵活性(在将对象传递给函数之前,您可以为对象设置一些状态,并且对象的
操作符()
可以使用该状态),但在您的情况下,函数指针可能同样好。

您需要一个函数指针。如果先使用
typedef
函数指针,则使用函数指针会更容易

typedef void (*FuncToCallType)();

FuncToCallType globalFunction; // a variable that points to a function

void setFuncToCall(FuncToCallType func) {
    globalFunction = func;
}

void testFuncCall() {
    globalFunction();
}

setFuncToCall( funcToCall ); //Set function to call,NOTE: no parentheses - just the name 
testFuncCall(); //Call the function that has been defined
正如其他答案所建议的那样,您也可以使用函数之类的对象。但这需要运算符重载(即使它仍然对您隐藏),并且通常与模板一起使用。
它提供了更大的灵活性(在将对象传递给函数之前,您可以为对象设置一些状态,对象的
操作符()
可以使用该状态),但在您的情况下,函数指针可能也一样好。

C函数指针的语法有点奇怪,但我们来看看:

// this is a global variable to hold a function pointer of type: void (*)(void)
static void (*funcp)(void); 
// you can typedef it like this:
//typedef void (*func_t)(void); 
// - now `func_t` is a type of a pointer to a function void -> void

// here `func` is the name of the argument of type `func_t` 
void setFuncToCall(void (*func)(void)) { 
// or just: void setFuncToCall(func_t func) {
    //Define some variable with func
    ...
    funcp = func;
}

void testFuncCall(void) {
    //Call function that has been defined in the setFuncToCall
    funcp();
}

setFuncToCall(funcToCall);  // without () !
testFuncCall();

函数指针的C语法有点奇怪,但我们来看看:

// this is a global variable to hold a function pointer of type: void (*)(void)
static void (*funcp)(void); 
// you can typedef it like this:
//typedef void (*func_t)(void); 
// - now `func_t` is a type of a pointer to a function void -> void

// here `func` is the name of the argument of type `func_t` 
void setFuncToCall(void (*func)(void)) { 
// or just: void setFuncToCall(func_t func) {
    //Define some variable with func
    ...
    funcp = func;
}

void testFuncCall(void) {
    //Call function that has been defined in the setFuncToCall
    funcp();
}

setFuncToCall(funcToCall);  // without () !
testFuncCall();

您要使用的是回调,并且已回答她:


我建议您使用
std::tr1::function
(通用回调)

您想要使用的是回调,并且已经回答了她:


<>我建议你使用<代码> STD::Trp:/Calp>(广义回调)

是的,我没有想到,这是C++ C++的一个更好的选择。是的,我还没有考虑过,它是C++的一个更好的选择,而不是我的答案。