这种功能如何起作用
How does that kind of function work?
我发现了一些奇怪的外观功能(希望我称呼正确(,并且无法真正理解其含义。也许您可以帮助我,并说出这是什么意思以及如何使用它?
int (*foo(const unsigned i))(const int, const int)
{
... // code
return some_function;
}
看起来像一个功能指针,但是我看到的指针更像是这样:
void foo(int x, double (*pf)(int)); // function using function pointer as a parameter
double (*pf)(int); // function pointer declaration
谢谢您的时间。
它定义了一个名为 foo
的函数,返回 函数指针。
foo
采用一个名为 i
的 const unsigned int
参数,并返回一个指针到一个函数,该函数采用两个 const int
s并返回 int
。
这个
int (*foo(const unsigned i))(const int, const int);
是具有名称foo
的功能声明,它将指针返回使用类型int(const int, const int)
的函数,并具有类型const unsigned int
的一个参数。
考虑到您可以删除const预选赛。那是这两个声明
int (*foo(const unsigned i))(const int, const int);
和
int (*foo(unsigned i))(int, int);
声明相同的一个功能。
您可以使用Typedef名称简化声明。
这是一个指示的程序
#include <iostream>
int (*foo(const unsigned i))(const int, const int);
int bar( int x, int y )
{
return x + y;
}
int baz( int x, int y )
{
return x * y;
}
int main()
{
std::cout << foo( 0 )( 10, 20 ) << std::endl;
std::cout << foo( 1 )( 10, 20 ) << std::endl;
return 0;
}
typedef int ( *fp )( int, int );
fp foo( unsigned i )
{
return i ? baz : bar;
}
其输出是
30
200
而不是Typedef声明
typedef int ( *fp )( int, int );
或
typedef int ( *fp )( const int, const int );
您也可以使用别名声明
using fp = int ( * )( int, int );
或
using fp = int ( * )( const int, const int );
甚至
using fp = int ( * )( const int, int );
和
using fp = int ( * )( int, const int );
相关文章:
- 为什么我的打印功能不起作用?链表
- Cuda 基本程序 (将值写入矩阵和 std:cout 不起作用) ;主功能不启动
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 虚拟功能不起作用
- C++插入排序错误功能不起作用
- 使用链表.为什么我的插入功能不起作用?
- 功能指针不起作用(C )
- 为什么功能对我不起作用?C
- C 所有质数功能不起作用
- 当直接分配时,为什么此功能指针分配起作用,而不是与有条件的操作员一起使用
- C 功能模板如何起作用
- 朋友功能不起作用(语法错误)
- 这种功能如何起作用
- Vigenere密码的C++功能有时才起作用(适用于某些输入,跳过其他输入的班次)
- Winpcap:暂停/停止嗅探pcap_breakloop功能不起作用
- 我的模板功能具有通用引用不起作用
- 疯狂(乘,添加,划分)哈希功能如何起作用
- 尽管我的功能和cout语句的放置错误,但我的程序为什么会起作用
- C 类是功能不起作用的参数
- 虚拟功能如何在标题,身体和派生类中起作用