64,642
社区成员
发帖
与我相关
我的任务
分享
你具体代码是什么?
int b = int(); //b = 0 ;
这个是调用int 的构造函数,初始为0的,所以b==0;这里返回的是结果给b
而不是说int()代表的是0值,他是一个函数
int V1(int ()); //就是這樣寫
typeid 輸出為 : int (*)( int (*)() )
也就是將參數中的 int() 解析為 int (*)()
而一開始我誤認為 int() = 0 從而解析成: int V1(0); //就錯了。想問為什麼。
----一样的道理,int()是一个函数调用,此是不能当结果0用的.
int V1() ; //V1()的参数为无,返回值int型
int (*V2)() ; //V2所指向的函数的参数也为空,返回值也是int型
//函数指针就是这么用的,比如:
#include <typeinfo>
#include <iostream>
using namespace std;
int V1()
{
return 0;
}
int main(int argc, char *argv[])
{
int (*V2)() ;
V2=V1;
cout <<typeid(V1).name() <<endl;
cout <<typeid(V2).name() <<endl;
int a(); //a = int(*)();//函數指針
int b = int(); //b = 0 ;
}
int func1();
int (*pfunc)() = func1;
MyFunc(func1());//先调用func1,利用func1的返回值做MyFunc的参数
MyFunc(pfunc);//直接将函数地址传递给MyFunc,可以在函数中来调用。其实也可以写成MyFunc(func1);
int V1() ; //V1表示的是函数名,也即是函数的起始地址
int (*V2)() ; //V2代表函数指针,指向一个返回值为int,参数为空的函数