我想重写一个虚函数,并使用关键词
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;
}