括号内的单个星号"(*)" C++做什么?
What does the single asterisk inside parentheses "(*)" do in C++?
我找到了这样的代码:
typedef std::map<std::string, Example*(*)()> map_type;
和搜索了一段时间后,我仍然不能弄清楚(*)操作符到底是做什么的。有人有什么想法吗?
这里的父级用于施加优先级。
类型Example*(*)()
是指向函数的指针,返回指向Example
的指针。
如果没有父元素,就会有
Example**()
返回指向Example
的指针的函数
这是用于声明指向函数(您的例子)或数组的指针的语法。用声明
typedef Example* (*myFunctionType)();
这将使行变成
typedef std::map<std::string, myFunctionType> map_type;
完全等价于你给出的行。注意,Example* (*myFunctionType)()
和Example* (*)()
的区别只是省略了类型的名称。
这是一个函数指针声明。
这个 typepedef 表示std::map将字符串映射到函数指针,接收void并返回Example*。
你可以这样使用:
#include <string>
#include <map>
typedef int Example;
Example* myExampleFunc() {
return new Example[10];
};
typedef std::map<std::string, Example*(*)()> map_type;
int main() {
map_type myMap;
// initializing
myMap["key"] = myExampleFunc;
// calling myExampleFunc
Example *example = myMap["key"]();
return 0;
}
在c++ 11中,由于使用了模板typedefs,可以使复杂的指针类型更容易理解:
template<typename T> using ptr = T*;
typedef std::map<std::string, ptr<Example*()>> map_type;
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用