Virtual functions in C + +

Source: Internet
Author: User

A virtual function is a member function in a class that is defined in the following format:

Virtual returns the type function name (parameter table).

The keyword virtual indicates that the member function is a virtual function. Virtual is used only in class definitions, such as virtual functions that are defined outside the class, and cannot be added to virtual.

When a class member function of a class is defined as a virtual function, the function always retains the characteristics of the virtual function in all derived classes derived from the class. When overriding a virtual function in a derived class, you do not have to add the keyword virtual. However, the rewrite does not only have the same name, but its parameter table and return type are all the same as the virtual functions in the base class, otherwise there is an error in the binder.

It is always good to set all member functions as virtual functions in a class, but be aware of the following:

1. A virtual function defined in a derived class must have the same name as a virtual function in the base class and must be the same as the parameter table, the same return type. Otherwise, it is considered an overload, not a virtual function. If a base class pointer is returned in a base class, the return derived class pointer in the derived class is allowed, which is an exception.

2. Only member functions of a class can be described as virtual functions. Because virtual functions apply only to class objects that have an inheritance relationship.

3. Static member functions are common to all the same classes of objects, are not limited to an object, and cannot be used as virtual functions.

4. The static and dynamic types of a class object are the same, and when you implement dynamic polymorphism, you must use a pointer variable or reference of the base class type so that the pointer points to an object of the different derived classes of the base class and point to the virtual function to achieve dynamic polymorphism

5. Inline functions Each object is a copy, no mapping relationship, and cannot be a virtual function.

6. A destructor can be defined as a virtual function, and the constructor cannot be defined as a virtual function because the object has not yet been instantiated at the time of the constructor. When the memory space is allocated dynamically in the base class and its derived classes, the destructor must be defined as a virtual function to achieve polymorphism when the object is revoked.

7. Virtual functions are slightly slower to execute. In order to achieve polymorphism, the entry Address table of the corresponding virtual function is saved in each derived class, and the function invocation mechanism is also implemented indirectly. So polymorphism always has to pay a price, but universality is a higher goal.

8. If the definition is placed outside the class, virtual can only be added to the function declaration before it can be added to the function definition. The correct definition must not include virtual.

A pure virtual function is a virtual member function that is marked as not specifically implemented. It is used when defining a base class and encountering a specific implementation that cannot define a virtual function in the base class, and its implementation relies on different derived classes. The general format for defining pure virtual functions is:

Virtual return type function name (parameter table) = 0;

A base class containing pure virtual functions cannot be used to define objects. Pure virtual functions do not have partial implementations. Cannot produce objects, so classes that contain pure virtual functions are abstract classes.

The definition of pure virtual functions must be noted:

1. When you define a pure virtual function, you cannot define the implementation part of a virtual function. Even if the function body is empty, the function body is empty and can be executed, but it is returned without doing anything. The pure virtual function cannot be called.

2. "= 0" indicates that the programmer will not define the function, and the function declaration retains a position for the derived class. "= 0" is essentially a pointer to the body of a function that is set to null.

3. In a derived class there must be a function body of a redefined pure virtual function, such that a derived class can be used to define an object.

Virtual functions are usually associated with dynamic linking. A link is a process in which a computer program itself is associated with one another, a process of associating an identifier name with a storage address, that is, the process of combining a message with an object's operation. If you use a base-class pointer or reference to indicate a derived class object and use that pointer to call a virtual function (the member selector uses the arrowhead ","), the program dynamically (at run time) chooses the virtual function of the derived class, called the dynamic Union. Dynamic linking is also called hysteresis-linked, which corresponds to static linking.

If you use the object name and point member selection operator "." When you invoke a virtual function by referencing a specific object, the virtual function that is called is determined at compile time (called a static binder).

When the C + + compiler compiles a class with one or several virtual functions and its derived classes, a virtual function table is established for that class. The virtual function table causes the executor to correctly select the virtual function that should be used each time it executes.

Virtual Functions in C + +

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.