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.