我想传递一个函数作为参数与空指针的另一个功能,这是行不通的C++传递函数作为参数另一个函数void指针
#include <iostream>
using namespace std;
void print()
{
cout << "hello!" << endl;
}
void execute(void* f()) //receives the address of print
{
void (*john)(); // declares pointer to function
john = (void*) f; // assigns address of print to pointer, specifying print returns nothing
john(); // execute pointer
}
int main()
{
execute(&print); // function that sends the address of print
return 0;
}
事情是无效的函数指针我可能会成为像
#include <iostream>
using namespace std;
void print();
void execute(void());
int main()
{
execute(print); // sends address of print
return 0;
}
void print()
{
cout << "Hello!" << endl;
}
void execute(void f()) // receive address of print
{
f();
}
一个更简单的代码,但我知道wonna如果我可以使用空指针
它是为实现这样的事情
void print()
{
cout << "hello!" << endl;
}
void increase(int& a)
{
a++;
}
void execute(void *f) //receives the address of print
{
void (*john)(); // declares pointer to function
john = f; // assigns address of print to pointer
john(); // execute pointer
}
int main()
{
int a = 15;
execute(increase(a));
execute(&print); // function that sends the address of print
cout << a << endl;
return 0;
}
它怎么不工作? – mfontanini
'void execute(void * f())'在语法上不正确。 –
我知道,即时通讯要求更正 –