What does const mean in this case?

 class Person { int age; public: void display() const { cout << "тут что-то выводим " << age << endl; } } 

    2 answers 2

    This means that the function does not change the object itself (except for the data members of the class object declared with the mutable qualifier, which can be changed even in functions declared with the const qualifier) ​​for which it is called.

    Therefore, you can call this function, for example, for constant objects:

     const Person person = { 18 }; person.display(); 

    Or

     void f( const Person &person ) { person.display(); } //... Person person = { 18 }; f( person ); 

    That is, in this function, the pointer to this object is of type const Person *

    Keep in mind that the class definition must end with a semicolon:

     class Person { //... }; ^^^ 

      Try reading a book by Robert Laforet . It is very well described there :)

      In a nutshell, something like this: The object that you create cannot change data through this method.

       class Person { public: int age; void display() const { cout<<"тут что-то выводим"<<age<<endl; } } 

      For example, I add a couple of methods to your class.

       class Person { private: int age; public: void input (int new_age) { age = new_age; // передали какое-то значение. } void display(int new_age) const { age = new_age; // ПРИ КОМПИЛЯЦИИ ВЫЙДЕТ ОШИБКА, ПОТОМУ ЧТО МЕТОД КОНСТАНТНЫЙ. ЭТО ОЗНАЧАЕТ ЧТО ВНУТРИ ЭТОГО МЕТОДА Я НЕ МОГУ МЕНЯТЬ ПОЛЯ КЛАССА. } }