类名之后直接跟()是什么意思?

一道题目的答案程序如下,我不明白main()中的第一句话Foo()是什么意思:

#include <vector>
#include <iostream>
#include <algorithm>

using std::vector; using std::sort;

class Foo {
public:
    Foo sorted() && ;
    Foo sorted() const &;
private:
    vector<int> data;
};

Foo Foo::sorted() && {
    sort(data.begin(), data.end());
    std::cout << "&&" << std::endl; // debug
    return *this;
}

Foo Foo::sorted() const & {
        Foo ret(*this);
        sort(ret.data.begin(), ret.data.end());
        return ret;

    std::cout << "const &" << std::endl; // debug

                                         //    Foo ret(*this);
                                         //    ret.sorted();     // Exercise 13.56
                                         //    return ret;

    return Foo(*this).sorted(); // Exercise 13.57
}

int main()
{
    Foo().sorted(); 
    Foo f;
    f.sorted(); // call "const &"
}
阅读 3.7k
1 个回答

这是类的构造函数吧

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题