我正在尝试覆盖虚拟,但也使用关键字 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;
}
最佳答案
正确的语法应该是:
关于c++ - 带有尾随返回类型的 final、override、const 的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43560697/