Preliminary Exploration of virtual functions in C ++

Source: Internet
Author: User

The definition of virtual functions follows the following important rules:


1. if a virtual function appears in a base class and a derived class, it only has the same name, and the form parameter is different, or the return type is different, even if the virtual keyword is added, it will not be delayed.

2. Only member functions of a class can be described as virtual functions. Because Virtual functions are only suitable for class objects with inheritance relationships, common functions cannot be described as virtual functions.

3. static member functions cannot be virtual functions, because static member functions are not restricted to an object.

4. the inline function cannot be a virtual function, because the inline function cannot dynamically determine the position during running. Even if the virtual function is defined inside the class, the system still regards it as non-inline during compilation.

5. the constructor cannot be a virtual function, because during the construction, the object is still an undefined space. Only after the construction is complete, the object is an instance of a specific class.


6. destructor can be virtual functions and are generally known as virtual functions.

 

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.