virtual function and polymorphism of __ function

Source: Internet
Author: User
Tags function prototype

A member function called the keyword virtual is known as a virtual function . It is called Run-time polymorphism only if you use the same base class pointer to access a virtual function. A base class pointer can point to a derived class object without a type conversion, but can only access the members that the derived class inherits from the base class. The interpretation of a virtual function call relies on the type of object that invoked it, which automatically completes the type conversion of the This pointer when the base class pointer points to an object of different derived classes to access different implementations of the virtual function in the derived class.

Overloading a virtual function requires a function name, return type, number of arguments, parameter types and order exactly the same, if the function prototype is different, only the function name is the same, then C + + is considered a generic function overload, thereby losing the virtual attribute. A virtual function is a function that distinguishes interfaces only by the this pointer , and the "virtual" attribute of C + + is responsible only for converting the association type of the base class this pointer to the class type of the current object, rather than changing the properties of other parameters of the function, while the program is running. (Hunger: When declaring a virtual function in a derived class, you do not need to prefix the virtual keyword.) )

Constructors cannot be virtual functions. Because when creating a derived class object, you must start with the root of the class hierarchy and call the constructor of the base class one by one along the inheritance path, up to its own constructor, and you cannot "selectively" call the constructor. So there is no sense in making fictional functions, and the definition of fictional functions will result in grammatical errors. Destructors can be virtual. When a virtual destructor is used to dynamically establish a class object, it directs the delete operator to select the correct destructor call. The destructor for each base class should be declared as a virtual function.

A pure virtual function is a virtual function that is described in a base class that does not have an implementation definition in the base class (the function assignment is 0) and requires that all derived classes must have their own version defined. A base class with pure virtual functions is called an abstract class, and an abstract class has at least one pure virtual function. A derived class of an abstract class is still an abstract class if it does not define an implementation version for an inherited pure virtual function. Correspondingly, a derived class that defines a pure virtual function implementation version is called a concrete class (concrete).

Virtual functions and polymorphism enable a member function to produce different actions based on the type of the calling object (which of course requires a certain amount of time overhead), which gives the program a lot of flexibility. Polymorphism is particularly suitable for the implementation of layered software system, easy to define the generality of the problem abstract , the implementation of the definition of differences .



C + + virtual function table parsing

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.