【发布时间】:2021-02-08 18:39:52
【问题描述】:
最近我正在阅读 c++ 模板。附录 B.3.4 中有一个模棱两可的例子,我添加了一些细节来编译。
#include <stdio.h>
typedef void FuncType(double, int);
void f(double, int) {
printf("call %s\n", __func__);
}
class IndirectFunctor {
public:
void operator()(double, double) {
printf("call %s\n", __func__);
}
operator FuncType*() const { return &f; }
};
void activate(IndirectFunctor const& funcObj) {
funcObj(3, 5);
}
int main(int argc, char* argv[]) {
IndirectFunctor funcObj;
activate(funcObj);
return 0;
}
它说当一个类有一个转换为函数运算符时,一个带有隐式参数的代理函数将被添加到重载决议集合中,因此调用operator FuncType*() const 需要将IndirectFunctor& 转换为优先级不高于成员的FuncType* operator()(double, double).
但是代码调用operator FuncType*() const,为什么operator FuncType*() const的优先级高于operator()(double, double)?
【问题讨论】:
标签: c++ overloading