繁体   English   中英

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

[英]How do you return a copy from a static member function?

我的静态topStock方法有什么问题? 它引用了Stock和Stock t。 它不应该返回s或t的副本吗?

错误:“。”之前的预期主表达式 令牌|

#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;
}

更改

Stock.topStock(s, y);

Stock::topStock(s, y);

因为它是静态成员函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM