Which functions cannot be declared as virtual functions?

Source: Internet
Author: User

I. First, let's review what virtual functions are and their functions to better understand what functions cannot be declared or defined as virtual functions.:

1. Definition:

A virtual function must be a non-static member function of the base class. Its access permission can be protected or public. The general form of defining a virtual function in the class definition of the base class is as follows:

Virtual function return value type virtual function name (parameter table) {function body}

2. role:

The role of a virtual function is to implement dynamic association, that is, to dynamically select an appropriate member function in the running stage of the program. After defining a virtual function, you can redefine the virtual function in the derived class of the base class. The newly defined function in the derived class should have the same number and type of parameters as the virtual function to achieve a unified interface, different Definition processes. If the virtual function is not redefined in the derived class, it inherits the virtual function of its base class.

When the program finds the keyword virtual before the virtual function name, it automatically uses it as a dynamic concatenation processing, that is, it dynamically selects the appropriate member function when the program is running.

3. Usage:

Dynamic Association stipulates that virtual functions can only be called by referring to the pointer of the base class or the reference of the base class object. The format is as follows:

Pointer variable name of the base class-> virtual function name (real parameter table)

Or

Reference name of the base class object. virtual function name (real parameter table)

4. Other Instructions:

Virtual functions are a manifestation of C ++ polymorphism:

For example, a subclass inherits a function (method) of the parent class, and we point the pointer of the parent class to the subclass, we must put the function (method) of the parent class) set to virtual (virtual function ). By using virtual functions, we can flexibly bind resources dynamically, at a certain cost. If the function (method) of the parent class is not necessary or cannot be implemented at all, you can set this function (method) to depend entirely on the subclass for implementation) set to virtual function name = 0. We call such a function (method) a pure virtual function. If a class contains pure virtual functions, this class is called an abstract class.
.

2. What functions cannot be declared as virtual functionsQuantity:

It is always helpful to set all member functions as virtual functions as much as possible in a class.
Note the following when setting virtual functions:
1: Only member functions of the class can be described as virtual functions;
2: static member functions cannot be virtual functions;
3: The inline function cannot be a virtual function;
4: The constructor cannot be a virtual function;
5: destructor can be virtual functions and are usually declared as virtual functions.

The member functions defined in the class are inline, but can still be virtual functions. Is there a problem with the sentence "inline functions cannot be virtual functions, should it be changed to "explicitly defined inline functions cannot be virtual functions ". For example, the following sample program:

# Include <iostream>
Using namespace STD;

Class base {
Public:
Virtual void F1 () {cout <"father" <Endl ;}
};
Class drived1: public base {
Public:
Void F1 () {cout <"son1" <Endl ;}
};
Class drived2: public base {
Public:
Void F1 () {cout <"son2" <Endl ;}
};

Void myprint (base * PBS ){
PBS-> F1 ();
}

Int main ()
{
Base father;
Drived1 son1;
Drived2 son2;
Myprint (& father );
Myprint (& son1 );
Myprint (& son2 );

System ("pause ");
Return 0;
}
Output:
Father
Son1
Son2

You can find that although F1 is defined in the base class, it should be an inline function, but it can still be a virtual function.
Number. The member functions defined in the class (the function body is in the class) can become virtual functions. Most compilers can automatically change functions declared as inline but not inline. Whether the compiler will implement the inline and virtual functions in the same way depends on the compiler and optimization methods. To become a virtual function, you must be able to get the address. inline functions cannot get the address, so they cannot become virtual functions.

If you write inline virtual void F (), the F () function must be inline. You can only ensure that F () is a virtual function (so that this function must not be an inline function)

For questions:
Can inline functions become virtual functions?
The answer is no. The problem is that you cannot determine whether a function is inline or not. The inlien keyword is just a suggestion for the compiler: "If possible, please turn this function into inline"

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.