const member variables and member functions in C++

In a class, if you don't want certain data to be modified, you can use constkeywords to qualify it. const can be used to modify member variables and member functions.

const member variable

The usage of const member variables is similar to that of ordinary const variables, only the const keyword needs to be added when declaring. There is only one way to initialize const member variables, which is through the initialization list of the constructor. This has been mentioned earlier, please click "C++ Initialization List" to review.

const member function (constant member function)

A const member function can use all member variables in the class, but cannot modify their values. This measure is mainly set for data protection. A const member function is also known as a const member function.

We usually set the get function as a constant member function. The names of functions that read member variables usually getstart with , followed by the name of the member variable, so they are usually called get functions.

Constant member functions need to add the const keyword at the end of the function header when declaring and defining , please see the following example:

    class Student{
    public:
        Student(char *name, int age, float score);
        void show();
        //声明常成员函数
        char *getname() const;
        int getage() const;
        float getscore() const;
    private:
        char *m_name;
        int m_age;
        float m_score;
    };
    Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){ }
    void Student::show(){
        cout<<m_name<<"的年龄是"<<m_age<<",成绩是"<<m_score<<endl;
    }
    //定义常成员函数
    char * Student::getname() const{
        return m_name;
    }
    int Student::getage() const{
        return m_age;
    }
    float Student::getscore() const{
        return m_score;
    }

The functions of the three functions getname(), getage(), and getscore() are very simple, just to obtain the value of the member variable, without any attempt to modify the member variable, so we added const restriction, which is a kind of insurance practice, but also make the semantics more obvious.

It should be emphasized that the const keyword must be added at the declaration and definition of the member function. char *getname() constand char *getname()are two different function prototypes. Adding const in only one place will cause conflicts between the function prototypes at the declaration and definition places.

Finally, let's distinguish the position of const:

  • The const at the beginning of the function is used to modify the return value of the function, indicating that the return value is of const type, that is, it cannot be modified, for example const char * getname().

  • Adding const at the end of the function header indicates a constant member function, which can only read the value of the member variable, but cannot modify the value of the member variable, for example char * getname() const.

Guess you like

Origin blog.csdn.net/shiwei0813/article/details/132515738