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()'實現。 –