Difference between a virtual function and a pure virtual function [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:51:02

问题:

Possible Duplicate:
C++ Virtual/Pure Virtual Explained

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 

回答1:

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 type of 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:

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.

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.)


Note that C++11 brought a new use for the delete and default keywords which looks similar to the syntax of pure virtual functions:

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

See this question and this one for more info on this use of delete and default.



回答2:

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 =0 at 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.



回答3:

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:

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 override keyword, the compiler will ensure that there is a base class virtual function with the same signature.



回答4:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!