Considerations for using Polymorphism

Source: Internet
Author: User

Polymorphism is implemented using virtual functions, the virtual function is used to inherit the hierarchy of the parent class and subclass, in addition to the base class and the subclass of the function name must be the same, the same parameter type, number and order of the same, that is, the parent class and the subclass of the virtual function can not just name overloading, but its declaration to be identical. Otherwise, no polymorphism can occur, even if you mark virtual.

An exception to the case, the return type exception.

VOID fn (int);
INT fn (int);

If the virtual function of the parent class and the subclass is as different as the two functions above, it can be polymorphic.


Some limitations of virtual functions:

1. Only member functions of a class can be declared as virtual functions

Because virtual functions only apply to class objects that have an inheritance relationship, ordinary functions cannot be declared as virtual functions.

2. A static member function cannot be a virtual function

Because static member functions are not bound by an object, even if the form is bundled, there is actually no information about the object, only the class information.

3. An inline function cannot be a virtual function

Because an inline function cannot dynamically determine its position in the run.

4. A constructor cannot be a virtual function

Because the object is still an amorphous virgin territory when constructed, the object can become a veritable object of a class only after the construction is complete.

5 destructors can be virtual functions and are typically declared as virtual functions.


1

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.