Statements for constants

    1. #define VALUE 100
    2. const int value = 100;
    3. const int * p_int;
    4. int const * p_int;
    5. int * const p_int;
    6. void func(const int *);
    7. void func(const int &);

    const member variables behavior similar with normal const variables
    const member functions promise not to modify member variables.

    1. class Student
    2. {
    3. private:
    4. const int BMI = 24;
    5. // ...
    6. public:
    7. Student()
    8. {
    9. BMI = 25; //can it be modified? no
    10. // ...
    11. }
    12. int getBorn() const
    13. {
    14. born++; //Can it be modified?
    15. return born;
    16. }
    17. };

    static members
    static members are not bound to class instances
    静态成员是不绑定到某一个实例的

    1. class Student
    2. {
    3. private:
    4. static size_t student_total; // declaration only
    5. public:
    6. Student()
    7. {
    8. student_total++;
    9. }
    10. ~Student()
    11. {
    12. student_total--;
    13. }
    14. static size_t getTotal() {return student_total;}
    15. };
    16. // definition it here
    17. size_t Student::student_total = 0;

    image.png