未显式指定要在派生类中重写的方法.这应该行得通吗

Not explicitly specifying method to be overriden in derived class. Is this supposed to work?

本文关键字:方法 重写 行得通 派生      更新时间:2023-10-16

用代码比用文字更好地解释:

//Classes.hpp
struct Base
{
   virtual void foo() = 0;
};
struct Derived : public Base
{
   //Nothing here
};
//Classes.cpp
void Derived::foo()
{
   //Do something here
}

我看到这个编译没有错误,但奇怪的是,你不必在Derived类中明确声明你要实现"foo"。

这应该按照C++标准工作吗?

不知道您使用的是什么编译器,但这不是合法的c++。我的VC和gcc在编译此代码时返回预期的错误。