0
我在Base
类中有一个方法,该方法返回对this
的取消引用。我想在Derived
类中使用这种方法,但也可以稍微扩展一下。这个例子会为自己说话:将取消引用返回到“this”的替代方法
#include <iostream>
class Base {
private:
int value = 0;
public:
int getValue() { return value; }
virtual Base& increase() {
value++;
return *this;
}
};
class Derived : public Base {
public:
Derived& increase() {
Base::increase();
if (getValue() == 1) std::cout << "Success" << std::endl;
return *this;
}
};
据我了解,在上述实施Base::increase();
只会在一个临时分配Base
对象增加一些value
。我该如何解决它?
没有,这里没有临时对象。 'Base :: increase()'意味着从'Base'类中调用'increase()'实现。 –