A brief analysis of virtual function _c language in C + +

Source: Internet
Author: User

I. Definition
Definition: A member function declared as virtual in a base class and redefined in one or more derived classes.

Syntax: virtual function return type function name (parameter table) {function Body}

Use: To achieve polymorphism, by pointing to the base class pointer of a derived class, accessing the virtual function of the same name in a derived class that has the same title as the overridden member function must be a non-static member function of the base class whose access can be protected or public.

A function defined as virtual is a base class that expects a derived class to redefine, and the base class expects that a function inherited by a derived class cannot be defined as a virtual function.

Second, the role
The function of virtual function is to realize dynamic interlock, that is, in the running phase of the program, dynamically selecting the appropriate member function, after defining the virtual function, you can redefine the virtual function in the derived class of the base class, and the function redefined in the derived class should have the same number of parameters and the formal parameter type as the virtual function. To achieve a unified interface, different definition process.  If a virtual function is not redefined in a derived class, it inherits the virtual function of its base class. When the program discovers the virtual function name, it will automatically be used as a dynamic binder, which is to select the appropriate member function dynamically when the program is run. Virtual functions are a manifestation of C + + polymorphism.

Using virtual functions, we are flexible to dynamically bind, of course, at the expense of a certain cost. If the function (method) of the parent class is not necessary or cannot be implemented, to be completely dependent on subclasses, you can set this function (method) to a virtual function name = 0 We refer to such a function (method) as pure virtual functions if a class contains pure virtual functions, this class is called an abstract class.

We simply set the member function of the base class to virtual, and the corresponding function of its derived class automatically becomes a virtual function.

Three, dynamic binding process
On a surface point, a virtual function invokes the version of a virtual function defined by a binding or pointing to a type based on the pointer to it or the type of the object to which the reference refers or binds.

Because a virtual function exists in a class, the compiler inserts a table for that class that you don't know about. That piece of data is called the vptr pointer, pointing to that table. That watch is called VTBL, each class has its own VTBL,VTBL function is to save a custom to virtual is the base class expecting a derived class redefined, the base class wants derived classes to inherit the function can not be defined as virtual function in the class of virtual functions of the address, we can image as an array of VTBL , each element of the array holds the address of the virtual function.

Call the virtual function, the first is to remove the value of vptr, this value is the address of VTBL, and then based on this value came to vtbl here, take out the VTBL in the corresponding slot value, this value is the address of the virtual function of the call, the last call this function. Now we can see that, as long as the vptr different, pointing to the VTBL is different, and different vtbl with the corresponding class of virtual function address, so the virtual function can complete its task.

Four, pure virtual function
A virtual function cannot be given a meaningful implementation in a base class, but it is declared as a pure virtual function, and its implementation is left to the derived class of the base class. This is the function of pure virtual functions.

A pure virtual function allows a class to have an action name first, without manipulating the content, so that the derived class is given a specific definition when inheriting. A class that contains pure virtual functions is called an abstract class. Such a class cannot declare an object, but serves as a derived class as a base class. A derived class also becomes an abstract class and cannot instantiate an object unless it fully implements all of the pure virtual functions in the base class in the derived class. Writes the =0 after the function parameter to specify a pure virtual function.

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: info-contact@alibabacloud.com 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.