由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - pointer to function
相关主题
这个function pointer最后的那个int是什么意思?问个char*的问题
[合集] (c++)为什么不能把这个function的definition放到class里What is wrong with the constructor calling?
What happens when recursion functions are declared inline?What is wrong with the code?
请教函数 INIT 怎么能free memoryC++里get array size的问题 (转载)
[合集] C++问题(copy constructor)大侠给解释下c++为何会允许这种polymorphism?
c++ typedef 一问谁给新手解释一下这个c++小程序
c++环境入门问题请教c++的string vector问题,谢谢! (转载)
一个指向指针的指针的引用?关于 VC++ vitual, reload 和 derive的一个问题...
相关话题的讨论汇总
话题: ft话题: func话题: typedef话题: char话题: void
进入Programming版参与讨论
1 (共1页)
n**d
发帖数: 9764
1
1. why do both a[0] and a[1] work?
2. why do both a[0]('a') and (*a[0])('a') work?
#include
using namespace std;
typedef void FT(char);
typedef void (*FT_p)(char);
void func(char ch)
{ cout << "func is called by [" << ch << "]" << endl; }
int main()
{
FT *a[2];
a[0] = func;
a[0]('a');
(*a[0])('a');
a[1] = &func;
a[1]('a');
(*a[1])('a');
}
r*********r
发帖数: 3195
2
there are implicit type conversions between a function name and
the corresponding function pointer both ways.
&, * are therefore both optional.
P********e
发帖数: 2610
3

// this is short form.
// this is also the short form
// this is the correct syntax statement.
// this is the right form.

【在 n**d 的大作中提到】
: 1. why do both a[0] and a[1] work?
: 2. why do both a[0]('a') and (*a[0])('a') work?
: #include
: using namespace std;
: typedef void FT(char);
: typedef void (*FT_p)(char);
: void func(char ch)
: { cout << "func is called by [" << ch << "]" << endl; }
: int main()
: {

n**d
发帖数: 9764
4
I see. Thanks!
We use typedef to declare FT as a type of pointer to function and then use
FT to declare a[2]. If we don't use typedef, how could we declare elements
of a[2] as pointers to functions?

【在 P********e 的大作中提到】
:
: // this is short form.
: // this is also the short form
: // this is the correct syntax statement.
: // this is the right form.

n**d
发帖数: 9764
5
got the answer.
void (*func_p_a[2])(char);
func_p_a[0] = func;

【在 n**d 的大作中提到】
: I see. Thanks!
: We use typedef to declare FT as a type of pointer to function and then use
: FT to declare a[2]. If we don't use typedef, how could we declare elements
: of a[2] as pointers to functions?

1 (共1页)
进入Programming版参与讨论
相关主题
关于 VC++ vitual, reload 和 derive的一个问题...[合集] C++问题(copy constructor)
为什么我看不懂下面的code,是不是水平还不够?c++ typedef 一问
请问const myClass &src 和myClass const &src有什么区别?c++环境入门问题
0 < -1 ? A c++ question一个指向指针的指针的引用?
这个function pointer最后的那个int是什么意思?问个char*的问题
[合集] (c++)为什么不能把这个function的definition放到class里What is wrong with the constructor calling?
What happens when recursion functions are declared inline?What is wrong with the code?
请教函数 INIT 怎么能free memoryC++里get array size的问题 (转载)
相关话题的讨论汇总
话题: ft话题: func话题: typedef话题: char话题: void