我想覆蓋虛擬,但也使用關鍵字override
,final
和const
,尾隨返回類型。這個問題似乎是在派生類中,編譯器錯誤(說我沒有指定尾隨返回類型)不是太有用。代碼在這裏:https://wandbox.org/permlink/zh3hD4Ukgrg6txyE最後一個,覆蓋,常量與尾隨返回類型的語法
並粘貼在下面。我已經玩過不同的順序,但仍似乎無法得到正確的結果。任何幫助將不勝感激,謝謝。
#include<iostream>
using std::cout; using std::endl; using std::ostream;
//////////////////////////////////////////////
//Base stuff
class Base
{
public:
Base(int i=2):bval(i){}
virtual ~Base()=default;
virtual auto debug(ostream& os=cout)const->ostream&;
private:
int bval=0;
};
auto Base::debug(ostream& os) const->ostream&
{
os << "bval: " << bval << endl;
return os;
}
///////////////////////////////////////////////
//Derived stuff
class Derived : public Base
{
public:
Derived(int i=2,int j=3):Base(i), dval(j){}
~Derived()=default;
auto debug(ostream& os=cout) const override final->ostream&; // error here
private:
int dval=0;
};
auto Derived::debug(ostream& os) const override final->ostream&
{
os << "dval: " << dval << endl;
return os;
}
///////////////////////////////////////////////
//Testing!
int main()
{
Base b(42);
b.debug()<<endl;
return 0;
}
小一點,你不需要〜衍生()=默認值;對? – Sheen
確實,這不是必需的:) – pss