What is the use of const in virtual isempty () const = 0?

Source: Internet
Author: User
After adding a const, this function will not change the value of the member function! This function can be accessed by constant objects (const ). Note that the functions with const and without const are two different functions! Use an example to illustrate the differences: # include <iostream> Class A {public: void empty () {STD: cout <"Call empty () \ n ";} void empty () const {STD: cout <"Call empty () const \ n" ;}}; void main () {const A; // a constant object, it can only access constant function. empty (); // access empty () const; If empty () const function is not found in A, the compiler reports a B; // a non-constant object B. empty (); // access empty ()}
 
 
 
Virtual void print () const = 0; (1) = 0 indicates that it is a pure virtual function (2) const indicates that its data member cannot be modified
 
 
 record it yourself: const and = 0 are irrelevant. To understand them separately, use const to modify the member functions, in general, the member variables of the class cannot be modified in this function. Unless the member variable is mutable, = 0 indicates that the member function is a pure virtual function, that is, it can be undefined, only interfaces are defined by their inherited classes. Of course, you can also define the default function body. If a class contains pure virtual functions with a value of 0, this class is an abstract class. It cannot be instantiated (its objects cannot be created), but can only be used to derive subclasses. 

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.