C++ 虚函数和纯虚函数的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2652198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:14:06  来源:igfitidea点击:

Difference between a virtual function and a pure virtual function

c++functionpure-virtual

提问by Sachin Chourasiya

What is the difference between a pure virtual function and a virtual function?

纯虚函数和虚函数有什么区别?

I know "Pure Virtual Function is a Virtual function with no body", but what does this mean and what is actually done by the line below:

我知道“纯虚函数是一个没有实体的虚函数”,但这意味着什么以及下面这行实际做了什么:

virtual void virtualfunctioname() = 0

回答by sbi

A virtual function makes its class a polymorphic base class. Derived classes can override virtual functions. Virtual functions called through base class pointers/references will be resolved at run-time. That is, the dynamic typeof the object is used instead of its static type:

虚函数使其类成为多态基类。派生类可以覆盖虚函数。通过基类指针/引用调用的虚拟函数将在运行时解析。也就是说,使用对象的动态类型而不是其静态类型

 Derived d;
 Base& rb = d;
 // if Base::f() is virtual and Derived overrides it, Derived::f() will be called
 rb.f();  

A pure virtual function is a virtual function whose declaration ends in =0:

纯虚函数是声明以 结尾的虚函数=0

class Base {
  // ...
  virtual void f() = 0;
  // ...

A pure virtual function implicitly makes the class it is defined for abstract(unlike in Java where you have a keyword to explicitly declare the class abstract). Abstract classes cannot be instantiated. Derived classes need to override/implement all inherited pure virtual functions. If they do not, they too will become abstract.

纯虚函数隐式地将它定义为抽象类(与在 Java 中使用关键字显式声明抽象类不同)。抽象类不能被实例化。派生类需要覆盖/实现所有继承的纯虚函数。如果不这样做,它们也会变得抽象。

An interesting 'feature' of C++ is that a class can define a pure virtual function that has an implementation. (What that's good for is debatable.)

C++ 的一个有趣的“特性”是一个类可以定义一个具有实现的纯虚函数。(这有什么好处值得商榷。)



Note that C++11 brought a new use for the deleteand defaultkeywords which looks similar to the syntax of pure virtual functions:

请注意,C++11 为deleteanddefault关键字带来了新用法,它看起来类似于纯虚函数的语法:

my_class(my_class const &) = delete;
my_class& operator=(const my_class&) = default;

See this questionand this onefor more info on this use of deleteand default.

这个问题,并且这一个更多的信息在这个使用deletedefault

回答by Naveen

For a virtual function you need to provide implementation in the base class. However derived class can override this implementation with its own implementation. Normally , for pure virtual functions implementation is not provided. You can make a function pure virtual with =0at the end of function declaration. Also, a class containing a pure virtual function is abstract i.e. you can not create a object of this class.

对于虚函数,您需要在基类中提供实现。然而,派生类可以用它自己的实现覆盖这个实现。通常,不提供纯虚函数实现。您可以=0在函数声明的末尾使函数成为纯虚函数。此外,包含纯虚函数的类是抽象的,即您不能创建此类的对象。

回答by Johann Gerell

A pure virtual function is usually not (but can be) implemented in a base class and must be implemented in a leaf subclass.

纯虚函数通常不会(但可以)在基类中实现,而必须在叶子类中实现。

You denote that fact by appending the "= 0" to the declaration, like this:

您可以通过将“= 0”附加到声明来表示该事实,如下所示:

class AbstractBase
{
    virtual void PureVirtualFunction() = 0;
}

Then you cannot declare and instantiate a subclass without it implementing the pure virtual function:

那么你不能在没有实现纯虚函数的情况下声明和实例化一个子类:

class Derived : public AbstractBase
{
    virtual void PureVirtualFunction() override { }
}

By adding the overridekeyword, the compiler will ensure that there is a base class virtual function with the same signature.

通过添加override关键字,编译器将确保存在具有相同签名的基类虚函数。

回答by AshleysBrain

You can actually provide implementations of pure virtual functionsin C++. The only difference is all pure virtual functions must be implemented by derived classes before the class can be instantiated.

您实际上可以在 C++ 中提供纯虚函数的实现。唯一的区别是所有纯虚函数必须由派生类实现,然后类才能被实例化。