C++ 运算符()重载和函数对象中的c'tor歧义

C++ 运算符()重载和函数对象中的c'tor歧义,c++,function,C++,Function,假设我们有下一个函数对象: class foo{ private: int counter; public: foo(int counter): counter(counter){} foo& operator=(const foo& ){...} bool operator() (int variable){....} } int main(){ foo f(4); foo x(5); x = f(4); r

假设我们有下一个函数对象:

class foo{
private:
    int counter;
public:

    foo(int counter): counter(counter){}
    foo& operator=(const foo& ){...}
    bool operator() (int variable){....}

}

int main(){
    foo f(4);
    foo x(5);
    x = f(4);
    return 0;
 }
编译器如何知道如何响应: x=f5?
我在web和Stack中搜索了一段时间,没有找到确切的答案,如果是转发,请告诉我,我将删除该问题。

这取决于5是用于构造对象还是调用已存在的对象:

foo f(5); // calls the constructor
f(5);     // calls operator()

我添加了一个名为eval的简单方法来解释它:

class foo {
private:
    int counter;

public:

    foo(int counter): counter(counter) {}

    bool operator() (int variable) {
        return variable < counter;
    }

    bool eval(int variable) {
        return variable < counter;
    }
};
您还可以使用运算符:

foo f = foo(5); // create an instance of `foo`
f(3); // returns true    ->   3 < 5
f(0); // returns false   ->   6 < 5
注:

你也可以写,但不要写:

foo f = foo(5); // create an instance of `foo`
f.operator()(3); // returns true    ->   3 < 5
f.operator()(0); // returns false   ->   6 < 5
它只是说,f不是声明的。无论如何,我看不出模糊性什么是f?请提供一份可能的副本
foo f = foo(5); // create an instance of `foo`
f.operator()(3); // returns true    ->   3 < 5
f.operator()(0); // returns false   ->   6 < 5