简体   繁体   English

是否需要在派生类中实现所有虚函数?

[英]Do ALL virtual functions need to be implemented in derived classes?

This may seem like a simple question, but I can't find the answer anywhere else. 这似乎是一个简单的问题,但在其他任何地方都找不到答案。

Suppose I have the following: 假设我有以下内容:

class Abstract {
public:
    virtual void foo() = 0;
    virtual void bar();
}

class Derived : Abstract {
public:
    virtual void foo();
}

Is it ok that class Derived does not implement the bar() function? 派生类不实现bar()函数可以吗? What if not ALL of my derived classes need the bar() function, but some do. 如果不是,我的所有派生类都需要bar()函数,但是有些需要。 Do all of the virtual functions of an abstract base class need to be implemented in the derived classes, or just the ones that are pure virtual? 是需要在派生类中实现抽象基类的所有虚函数,还是仅在纯虚函数中实现? Thanks 谢谢

Derived classes do not have to implement all virtual functions themselves. 派生类没有实现所有的虚函数本身。 They only need to implement the pure ones. 他们只需要实现纯代码 1 That means the Derived class in the question is correct. 1这意味着问题中的Derived类是正确的。 It inherits the bar implementation from its ancestor class, Abstract . 它从其祖先类Abstract 继承 bar实现。 (This assumes that Abstract::bar is implemented somewhere. The code in the question declares the method, but doesn't define it. You can define it inline as Trenki's answer shows, or you can define it separately.) (这假定Abstract::bar在某处实现。问题中的代码声明了方法,但未定义方法。您可以按Trenki的答案显示的方式内联定义它,也可以单独定义它。)


1 And even then, only if the derived class is going to be instantiated . 1即使如此,仅当派生类将被实例化时 If a derived class is not instantiated directly, but only exists as a base class of more derived classes, then it's those classes that are responsible for having all their pure virtual methods implemented. 如果派生类不是直接实例化的,而是仅作为更多派生类的基类存在的,则这些类负责实现其所有纯虚方法。 The "middle" class in the hierarchy is allowed to leave some pure virtual methods unimplemented, just like the base class. 就像基类一样,允许层次结构中的“中间”类保留一些未实现的纯虚拟方法。 If the "middle" class does implement a pure virtual method, then its descendants will inherit that implementation, so they don't have to re-implement it themselves. 如果“中间”类确实实现了纯虚拟方法,则其后代将继承该实现,因此他们不必自己重新实现它。

Only the pure virtual methods have to be implemented in derived classes, but you still need a definition (and not just a declaration) of the other virtual methods. 仅纯虚拟方法必须在派生类中实现,但是您仍然需要其他虚拟方法的定义(而不仅仅是声明)。 If you don't supply one, the linker might very well complain. 如果您不提供,则链接器可能会抱怨。

So, just putting {} after your optional virtual method gives you an empty default implementation: 因此,只需将{}放在可选的虚拟方法之后,即可得到一个空的默认实现:

class Abstract {
public:
    virtual void foo() = 0; // pure virtual must be overridden
    virtual void bar() {}   // virtual with empty default implementation
};

class Derived : Abstract {
public:
    virtual void foo();
};

A more involved default implementation would go into a separate source file though. 不过,涉及更多的默认实现将放入单独的源文件中。

The ISO C++ Standard specifies that all virtual methods of a class that are not pure-virtual must be defined. ISO C ++标准规定必须定义非纯虚拟类的所有虚拟方法。

Simply put the rule is: 简单地说,规则是:
If your derived class overiddes the Base class virtual method then it should provide a definition as well, If not then the Base class should provide the definition of that method. 如果您的派生类覆盖了基类虚方法,则它也应提供一个定义;否则,基类应提供该方法的定义。

As per the above rule in your code example, virtual void bar(); 根据代码示例中的上述规则, virtual void bar(); needs a definition in the Base class. 需要在基类中定义。

Reference: 参考:

C++03 Standard: 10.3 Virtual functions [class.virtual] C ++ 03标准:10.3虚函数[class.virtual]

A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both; 必须定义在类中声明的虚函数,或在该类中声明为纯(10.4),或同时声明两者。 but no diagnostic is required (3.2). 但不需要诊断(3.2)。

So either you should make the function pure virtual or provide a definition for it. 因此,您应该使函数成为纯虚函数,或者为其提供定义。

The gcc faq doccuments it as well: gcc常见问题解答也记录了它:

The ISO C++ Standard specifies that all virtual methods of a class that are not pure-virtual must be defined, but does not require any diagnostic for violations of this rule [class.virtual]/8 . ISO C ++标准规定,必须定义非纯虚拟类的所有虚拟方法,但是对于违反此规则[class.virtual]/8情况,不需要进行任何诊断。 Based on this assumption, GCC will only emit the implicitly defined constructors, the assignment operator, the destructor and the virtual table of a class in the translation unit that defines its first such non-inline method. 基于此假设,GCC将仅在隐式定义的构造函数,赋值运算符,析构函数和定义其第一个此类非内联方法的转换单元中的类的虚拟表中发出该表。

Therefore, if you fail to define this particular method, the linker may complain about the lack of definitions for apparently unrelated symbols. 因此,如果您未能定义此特定方法,则链接器可能会抱怨缺少针对显然不相关的符号的定义。 Unfortunately, in order to improve this error message, it might be necessary to change the linker, and this can't always be done. 不幸的是,为了改善此错误消息,可能有必要更改链接器,但并非总是如此。

The solution is to ensure that all virtual methods that are not pure are defined. 解决方案是确保定义了所有非纯虚拟方法。 Note that a destructor must be defined even if it is declared pure-virtual [class.dtor]/7 . 请注意,即使已将析构函数声明为纯虚拟[class.dtor]/7 ,也必须对其进行定义。

是的,那很好。您只需要实现任何纯虚函数即可实例化从抽象基类派生的类。

Yes, Its correct that a Derived class has to OVERRIDE the function which is Pure Virtual in the Parent Class. 是的,它的正确派生类必须重写父类中的纯虚函数。 Parent class having a Pure Virtual Function is called Abstract Class only because it's Child class must give their own body of the Pure Virtual Function. 具有纯虚函数的父类仅被称为“抽象类”,因为它的子类必须提供自己的纯虚函数的主体。

For the Normal Virtual Functions:- Its not necessary to override them further, as some child class may have that function, some may not have. 对于普通虚拟函数:-不必进一步覆盖它们,因为某些子类可能具有该功能,而某些子类可能没有。

Main purpose of Virtual Function mechanism is Run Time Polymorphism, whether main purpose of Pure Virtual Function(Abstract Class) is to make it mandatory to have the same name Function with own's body. 虚拟功能机制的主要目的是运行时多态性,无论纯虚拟功能(抽象类)的主要目的是否是要强制具有与自己的身体同名的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您是否需要从所有派生类调用虚拟基类构造函数? 即使他们不是最衍生的? - Do you need to call virtual base class constructor from all derived classes? Even if they're not the most derived? 派生类中的虚函数 - Virtual Functions in Derived Classes 在派生类中使用虚函数 - Use of virtual functions in derived classes 如果使用vtable实现具有虚函数的类,那么如何实现没有虚函数的类? - If classes with virtual functions are implemented with vtables, how is a class with no virtual functions implemented? 派生类是否需要在基类上声明自己的虚函数版本? - Do derived classes need their own version of a virtual function declared on base? 有没有比在所有派生类都没有实现的基础中添加新的虚拟 function 更好的设计实践 - Is there a better design practice than to add a new virtual function in base that is not implemented by all derived classes 在不同类中实现的纯虚函数的继承 - Inheritance of pure virtual functions implemented in different classes 类派生形式结构上的虚函数 - Virtual functions on classes derived form structs 2个派生类的虚拟函数(十六进制+二进制) - Virtual functions for 2 derived classes (Hex + binary) 对虚函数和派生类的混淆 - Confusion over virtual functions and derived classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM