virtual function, pure virtual function, virtual function and destructor

Source: Internet
Author: User

One, virtual function

You cannot declare a normal function outside the class as a virtual function only if you declare the member function of the class with virtual to make it a virtual function. Because virtual functions are allowed to redefine a virtual function of a base class in a derived class. Therefore , virtual functions can only be used in the inheritance hierarchy of classes.

After a member function is declared as a virtual function, a class in the same family can no longer define a function of the same name that is not virtual but has the same parameters (both number and type) and function return value type as the virtual function.

What do you think about declaring a member function as a virtual function?

① See if the class that contains the member function will be used as the base class

② See if the member function has the ability to change after the class inherits, it should generally be declared as a virtual function if you want to change its functionality.

If a member function does not need to be modified after the class is inherited, or if the derived class does not use the function, do not declare it as a virtual function. Do not consider all member functions in a class to be declared as virtual functions just as a base class.

You should consider whether calls to member functions are accessed through the object name or through a base-class pointer or reference, and if accessed through a base-class pointer or reference, it should be declared as a virtual function. Sometimes when you define a virtual function, it does not define its function body, which is pure virtual functions. Its function simply defines a virtual function name, which is left to the derived class to add.

Description: using Virtual functions, the system must have a certain amount of space overhead . When a class has a virtual function, the compilation system constructs a virtual function table (VTBL) for the class, which is an array of pointers that holds the entry address for each virtual function. The time overhead of dynamic association is very low, and the efficiency of polymorphism is improved.

Second, pure virtual function

Sometimes a virtual function in a base class is defined for use in a derived class, and it has no meaning in the base class. Such functions are called pure virtual functions, which do not need to be written in the form of empty functions, but simply declared as:

virtual function type function name (formal parameter table column) = 0;

Note: There is no function body for pure virtual function;

The Last "= 0" does not mean that the return value of the function is 0, only the formal function, telling the compilation system "This is pure virtual function";

This is a declaration statement, and there should be a semicolon at the end.

A pure virtual function cannot be called unless it has the function's name and function. This function can be called after it has been defined in a derived class to function.

Third, virtual destructor function

The function of a destructor is to revoke the object of the class from memory before the object is revoked. usually the system executes only the destructor of the base class and does not perform the destructor of the derived class .

Only the destructor of the base class is declared as a virtual function, that is, the virtual destructor, so that when the base class object is revoked and the object of the derived class is revoked, the process is completed dynamically .

When a destructor for a base class is declared as a virtual function, the destructor for all derived classes derived from that base class is automatically made virtual, even if the destructor of the derived class differs from the destructor name of the base class.

It is better to declare the destructor of the base class as a virtual function, which will cause the destructor of all derived classes to automatically become virtual, and if the explicit delete operator in the program deletes an object, and the action object uses a pointer to the base class of the derived class object, the system invokes the destructor of the corresponding class.

Constructors cannot be declared as virtual functions.

For example:

#include <iostream>using namespace Std;class animal{public:animal () {cout << "animal::animal () is called" << Endl;}; Virtual ~animal () {cout << "animal::~animal () is called" << Endl;} virtual void Eat () {  cout << "Animal::eat () is called" << Endl;} virtual void Walk () {  cout << "Animal::walk () is called" << Endl;} /* Data */};class dog:public animal{public:dog (int w,int h) {cout << "Dog::D og () is called" << endl;this-> Weight=w;this->height=h;} Virtual ~dog () {cout << "Dog::~dog () is called" << Endl;} int weight;int height;void eat () {cout<< "I eat Meat" <<ENDL;} void Walk () {cout<< "Run" <<ENDL;} /* data */}; int main (int argc, char const *argv[]) {/* code */animal *ani= New Dog (12,23);D og *dog=new dog (23,34); Ani->eat (); ani-> ; Walk ();d og->eat ();d og->walk ();d elete ani;//delete Dog;return 0;}

(c + + destructors are generally defined as virtual functions) if the destructor is not defined as a virtual function in the base class, then the destructor of the parent class is called, and the child class is not called, and there is a memory leak if there is dynamic memory allocation in the constructor of the parent class and child class. A general destructor is best written as a virtual function, especially a parent class.

This article is for reference:

virtual function, pure virtual function, virtual function and destructor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.