Some things about C + + virtual functions

Source: Internet
Author: User

First, C + + polymorphism : The ability of the system to determine which overloaded member function to invoke, based on the object type at run time.

Polymorphism is achieved through virtual functions . A virtual function is added before the member function.

A class with virtual member functions, where the compiler automatically adds a pointer at the beginning of each object, called a virtual table pointer , because it points to a table, called a virtual function table , and the element of the table is a function pointer to the virtual member function code block of the class.

All objects of the class share a single table. For the concrete information of virtual table pointer and virtual function table, we can refer to the virtual function table of Hao's uncle.

The definition of a virtual function follows the following rules:

1if the virtual function appears in the base class and the derived class, only the name is the same , and the formal parameter or return type is different, then even if the virtual keyword is added, the polymorphism will not be implemented.
"Base class/derived class objects can only directly access their defined functions, although the virtual table of a derived class object has a virtual function pointer to the base class, but the derived class object cannot call the"2only member functions of a class can be described as virtual functions, because virtual functions are suitable only for class objects that have inheritance relationships , so ordinary functions cannot be described as virtual functions. 3.Staticmember functionscan'tis a virtual function,because a static member function belongs to a class, it does not belong to any object. A static member function cannot be a const member function because the compiler automatically inserts a const T *this parameter in the object's const function, and the static member function does not belong to the object "
4an inline (inline) function cannot be a virtual function. Even if the virtual function defines the definition within the class, the system still considers it to be non- inline when compiling.
"Inline functions may expand code at compile time so that the code area of the memory does not have the code of the function, is not a function of the concept, the natural virtual table can not save the function pointer."5The . Constructor cannot be a virtual function, because the object is a bit-trained space when constructed, and the object is an instance of the concrete class only after the construction is complete. 6. DestructorsIt can be a virtual function, and it is usually known as a virtual function.
"There are base classes of derived classes whose destructors must be virtual, so that when the destructor is destroyed, the derived class object is first refactored, and then the base class object is not refactored, otherwise the part of the derived class is not deconstructed"

See rule 1 above, there are three concepts to note:

overload (Heavy-duty): The semantics and functions of several functions are represented by the same name , but the < parameters > or < parameters are different from the return values > (the Number of parameters, type, or order ), that is, function overloading. (1) The same range (common functions within the same class or in the same file);2) function has the same name; (3) parameters are different; (4The virtual keyword is optional. Override (Overwrite): Refers to a derived class function that overrides a base class function and can be a virtual function only, characterized by: (1) different ranges (in the derived class and the base class, respectively);2) function has the same name ; (3) The return value is the same as the parameter ;4The base class function must have the virtual keyword. Overwrite (override): Refers to a function of a derived class that masks a base class function with the same name as the following rule: (1if the function of the derived class has the same name as the function of the base class, the parameters are different . Regardless  of the virtual keyword, the function of the base class is hidden (be careful not to confuse overloading). "Rule 1" (2if the function of the derived class has the same name as the function of the base class, and the parameters are the same , the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay). 

Some things about C + + virtual functions

Related Article

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.