C++ C+中函数指针的地址+;
我不清楚调用返回的值是什么:C++ C+中函数指针的地址+;,c++,function,pointers,C++,Function,Pointers,我不清楚调用返回的值是什么: &next, fp, *fp, &return_func_ptr, fp_ptr, &fp_ptr, *fp_ptr 它们似乎都给了我值1。这是什么意思 还有,我该如何申报 int (*return_f())(char) 要在不使用typedef的情况下接收参数 #include <iostream> int next(int n){ return n+99; } // returns pointer to
&next, fp, *fp, &return_func_ptr, fp_ptr, &fp_ptr, *fp_ptr
它们似乎都给了我值1
。这是什么意思
还有,我该如何申报
int (*return_f())(char)
要在不使用typedef的情况下接收参数
#include <iostream>
int next(int n){
return n+99;
}
// returns pointer to a function
typedef int (*fptr)(int); // using typdef
fptr return_func_ptr(){
return next;
}
int f(char){
return 0;
}
int (*return_f())(char){ // how do you pass a parameter here?
// std::cout << "do something with " << param << std::endl;
return f;
}
int main()
{
int x = 5;
// p points to x
int *p = &x;
std::cout << "x=" << x << std::endl; // 5, value of x
std::cout << "&x=" << &x << std::endl; // 0x7fff6447a82c, address of x
std::cout << "p=" << p << std::endl; // 0x7fff6447a82c, value of p is address of x
std::cout << "*p=" << *p << std::endl; // 5, value of x (p dereferenced)
std::cout << "&p=" << &p << std::endl; // 0x7fff6447a820, address of p pointer
// change value of x thru p
// p = 6; // error, can't set int* to int
*p = 6;
std::cout << "x=" << x << std::endl; // 6
int y = 2;
// int *q = y; // error can't initiate with type int, needs int*
// pointer to a function
int (*fp)(int);
std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp
std::cout << "fp=" << fp << std::endl; // 0, value of pointer fp
fp = &next; // fp points to function next(int)
fp = next;
std::cout << "&next=" << &next << std::endl; // 1, address of function?
std::cout << "fp=" << fp << std::endl; // 1, value is address of function?
std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp?
std::cout << "*fp=" << *fp << std::endl; // 1, address of function?
// calling function thru pointer
int i = 0;
i = (*fp)(i);
std::cout << "i=" << i << std::endl; // 99
i = fp(i);
std::cout << "i=" << i << std::endl; // 198
// function returns pointer to function
fptr fp_ptr = return_func_ptr();
std::cout << "&return_func_ptr=" << &return_func_ptr << std::endl; // 1
std::cout << "fp_ptr=" << *fp_ptr << std::endl; // 1
std::cout << "&fp_ptr=" << *fp_ptr << std::endl; // 1
std::cout << "*fp_ptr=" << *fp_ptr << std::endl; // 1
int j = fp_ptr(1);
std::cout << "j=" << j << std::endl; // 100
}
#包括
int next(int n){
返回n+99;
}
//返回指向函数的指针
typedef int(*fptr)(int);//使用typdef
fptr返回函数ptr(){
下一步返回;
}
int f(字符){
返回0;
}
int(*return_f())(char){//如何在此处传递参数?
//std::coutstd::coutstd::cout这里有一个似乎不清楚的指针:
// pointer to a function
int (*fp)(int);
std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp
std::cout << "fp=" << fp << std::endl; // 0, value of pointer fp
这里有两件事:
- 在我指出的那条线上,我不确定这是你想要测试的
- 另外,
cout
没有重载来获取函数指针,它将使用bool
来代替。因此它应该是:
std::cout << "fn_ptr=" << reinterpret_cast<void*>( fn_ptr ) << std::endl;
std::cout这里有一个似乎不清楚的指针:
// pointer to a function
int (*fp)(int);
std::cout << "&fp=" << &fp << std::endl; // 0x7fff66da6810, address of pointer fp
std::cout << "fp=" << fp << std::endl; // 0, value of pointer fp
这里有两件事:
- 在我指出的那条线上,我不确定这是你想要测试的
- 另外,
cout
没有重载来获取函数指针,它将使用bool
来代替。因此它应该是:
std::cout << "fn_ptr=" << reinterpret_cast<void*>( fn_ptr ) << std::endl;
std::coutfptrcast.cc:3:27:错误:从类型“fptr{aka int(*)(int)}”到类型“void*”static_cast(fp_ptr)的静态转换无效
reinterpret\u cast
可能会起作用。你能直接将函数指针转换为一个void*
吗?我想你至少需要一个reinterpret\u cast
是的,我已经解决了这个问题。@Devolus“fixed”会是一个更好的短语。fptrcast.cc:3:27:错误:无效的静态\u cast来自类型'fptr{aka int(*)(int)}“键入'void*'static_cast(fp_ptr);
重新解释_cast
可能会起作用。您可以直接将函数指针转换为void*
吗?我想您至少需要一个重新解释
强制转换。是的,您是对的,我已经修复了。@Devolus“修复”最好是一个更好的短语。你的第一个代码< STD::CUT是一个很好的习惯,编写更可读的代码。C++允许接口。你可以得到同样可读和可维护的结果。为什么要让生活变得复杂?你的第一个C++代码:STD::CUT是编写更可读的代码的好习惯。C++允许接口。可以获得可读性和可维护性相同的结果。那么为什么要让生活变得复杂呢?谢谢你的指点(没有双关语)所有这一切,对于这篇文章来说。另外,我认为“&return\u func\u ptr”会显示函数的地址。指出的行应该写“&fp\u ptr”我也必须重新解释吗?不,你不应该这样做。void&
没有任何意义……对void
的引用是不正确的。如果你想&fn\ptr
,您想要的是fn_ptr
的地址。它不一样。谢谢您的指点(没有双关语)所有这一切,对于这篇文章来说。另外,我认为“&return\u func\u ptr”会显示函数的地址。指出的行应该写“&fp\u ptr”我也必须重新解释吗?不,你不应该这样做。void&
没有任何意义……对void
的引用是不正确的。如果你想&fn\ptr
,您想要的是fn_ptr
的地址。它不一样。
std::cout << "fn_ptr=" << reinterpret_cast<void*>( fn_ptr ) << std::endl;