这个论点的声明是什么意思:"type (*name1) (const type& name2)"?
What this declaration of the argument mean: "type (*name1) (const type& name2)"?
在我需要修改的代码中,有一个函数的声明,它有几个参数。正如预期的那样,每个参数都以以下方式声明:arg_type arg_name
。但是,其中一个参数是以以下方式声明的:bool (*filter) (const row<aaa::bbb>& entity)
和我不理解此声明。
所以,我认为我们有一个可以具有布尔值的自变量,但(*filter)
和(const row<aaa::bbb>& entity)
是什么意思?
添加
为了更清楚,这里有更多的上下文:
void func(int a, double b, bool (*filter) (const row<aaa::bbb>& entity))
。
添加2
它是这样工作的吗?
bool myFilter( const row<aaa::bbb>& e) { /* ... */ }
bool applyFilter( bool (*filter)(const row<aaa::bbb>& entity, bool& x)){
return filter(x&entity);
}
applyFilter(myFilter entity, x);
bool (*filter) (const row<aaa::bbb>& entity)
filter
是指向一个函数的指针,该函数只接受const row<aaa::bbb>&
作为参数,并返回bool
。
示例
bool myFilter( const row<aaa::bbb>& e) { /* ... */ }
bool applyFilter( bool (*filter)(const row<aaa::bbb>&)){
return filter(somerow);
}
它声明了一个名为filter
的指针变量,它指向一个签名为bool(const row<aaa::bbb>&)
的函数(而不是像您可能习惯的那样指向一个对象(,这意味着一个函数通过const-ref获取row<aaa::bbb>
并返回bool
。
在C++中声明函数指针的语法通常是return-type (*variable-name)(argument-list)
,给您一个名为variable-name
的returntype (*)(argument-list)
类型的变量,这有点奇怪,但您已经习惯了
相关文章:
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 控制到达非空函数clang(-Wreturn-type)的末尾
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 将系数存储在头文件的数组中("does not name a type"错误)
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在"结构提升::enable_if<提升::is_pod<T>,无效>"中没有名为"type"的类型
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- OpenCV CV_16F type
- C++ "错误:在'类 std::result_of< ... >"中没有名为'type'的类型"
- 将内存分配返回值强制转换为 TYPE 数组
- C++ 一个lambda浅拷贝const Type&如果它被赋予一个命名捕获,如[copy=val](){}?
- reference_wrapper导致"incomplete type is not allowed"
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编
- 如何解决"'mutex' in namespace 'std' does not name a type"?
- 这个论点的声明是什么意思:"type (*name1) (const type& name2)"?