如何从静态成员函数返回副本?

时间:2013-09-18 19:57:36

标签: c++

我的静态topStock方法有什么问题?它引用了股票和股票t。难道它不会返回s或t的副本吗?

错误:在'之前预期的primary-expression。'令牌|

#include <iostream>

using namespace std;

class Stock {

public:

    Stock() : x(0){ }
    Stock(int val) : x(val){}

    void display() const;

    static Stock topStock(const Stock& s, const Stock& t) {
     if (s.x > t.x)
        return s;
    else
        return t;
    }

    int x;
};

void Stock::display() const
{

    std::cout << this->x;

}


int main()
{
    Stock s(9);
    Stock y(8);
    Stock z = Stock.topStock(s, y);
    return 0;
}

1 个答案:

答案 0 :(得分:6)

更改

Stock.topStock(s, y);

Stock::topStock(s, y);

因为它是静态成员函数。

相关问题