C + + virtual function, pure virtual function

Source: Internet
Author: User

1. Virtual functions and pure virtual functions can be defined in the same class, classes containing pure virtual functions are called abstract classes, and classes that contain only virtual functions cannot be called abstract classes.

2. Virtual functions can be used directly, or in the form of a multi-state overload, and the pure virtual function must be implemented in the subclass to be used, because the pure virtual function is declared in the base class and not defined.

3. Virtual and pure virtual functions can be overloaded in subclasses and called in polymorphic form.

4. Virtual functions and pure virtual functions usually exist in the abstract base class, and are inherited subclass overloads to provide a uniform interface.

5. The definition of virtual function: virtual{}; The definition of pure virtual function: virtual {} = 0; There cannot be a static identifier in the definition of a virtual function and a pure virtual function , for the simple reason that the static The modified function requires early binding at compile time, but the virtual function is dynamically bound , and the function life cycle of the two modifiers is not the same.

Virtual function fully embodies the two characteristics of inheritance and polymorphism in object-oriented thought, which is widely used in C + + language. For example, in the Microsoft MFC class Library, you will find that many functions have the virtual keyword, that is, they are virtual functions. No wonder some people even call virtual functions The essence of the C + + language .

A pure virtual function is defined so that the base class cannot be instantiated , since it is meaningless to instantiate such an abstract data structure itself or to give the implementation.

Pure virtual function is only an interface, is a function of the declaration, it is to be left in the subclass to achieve.

Virtual functions can also not be overloaded within subclasses, but pure virtual must be implemented in subclasses, just like Java interfaces. Usually we add a lot of functions to virtual, is a good habit, although sacrificing some performance, but increased the object-oriented polymorphism, because you can hardly anticipate the parent class inside the function is not to modify its implementation of the child class

C + + virtual function, pure virtual function

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.