重载后增量运算符

时间:2017-02-09 18:05:48

标签: c++ visual-c++ operator-overloading warnings

我使用Visual Studio 2015 Update 3并在编写一个简单的类时遇到以下警告,该类基本上只是一个int的包装。

以下示例不是实际的类,而是一个较短的示例,导致相同的警告:

class Foo {

public:

    Foo() : x(23) {}
    Foo(int xx) : x(xx) {}
    Foo(const Foo& other) : x(other.x) {}

    Foo& operator++(int junk) {
        Foo copy(*this);
        this->x += 5;
        return copy;
    }
    int x;
};

在第12行(return copy)中,我收到以下警告:

warning C4172: returning address of local variable or temporary: copy

我感到困惑,因为我认为我并没有真正回复地址或临时,因为我认为Foo的复制构造函数会处理这种情况。

如果有人能告诉我我在这里失踪了什么,我会很高兴。

我也发现了后增量运算符的实现就像这个 在其他stackoverflow文章中也是如此(例如C++: overloading ++ for both pre and post increment

提前谢谢大家。

0 个答案:

没有答案