1. const char* p = greeting; //指针可变,数据不可变
  2. char* const p = greeting; //指针不可变,数据可变
  3. const char* const p = greeting; //指针和数据均不可变
  4. //const出现在星号的左边,表示所指之物是不可变的
  5. //const出现在星号的右边,表示指针本身是不可变的
  • 欲阻止一个变量被改变,可以使用const关键字。在定义该const变量时,通常需要对它进行初始化,因为以后就没有机会再去改变它了;
  • 对指针来说,可以指定指针本身为const,也可以指定指针所指的数据为const,或二者同时指定为const;
  • 在一个函数声明中,const可以修饰形参,表明它是一个输入参数,在函数内部不能改变其值
  • 对于类的成员函数,若指定其为const类型,则表明其是一个常函数,不能修改类的成员变量;(相当于指定this指针为const, const *this)

    When you add the const keyword to a method the this pointer will essentially become a pointer to const object, and you cannot therefore change any member data. (Unless you use mutable, more on that later).

  • const修饰类的成员变量,表示成员常量,不能被修改。

  • 对于const函数的重载,const对象只能调用const函数,非const对象优先调用非const函数。
  • const函数只能调用const函数,非const函数可以调用const函数。
  • 类外定义的const成员函数,在定义和声明处都需要const修饰符。


  1. /
  2. class A
  3. {
  4. private:
  5. const int a; // 常对象成员,只能在初始化列表赋值
  6. public:
  7. // 构造函数
  8. A() : a(0) { };
  9. A(int x) : a(x) { }; // 初始化列表
  10. // const可用于对重载函数的区分
  11. int getValue(); // 普通成员函数
  12. int getValue() const; // 常成员函数,不得修改类中的任何数据成员的值
  13. };
  14. void function()
  15. {
  16. // 对象
  17. A b; // 普通对象,可以调用全部成员函数、更新常成员变量
  18. const A a; // 常对象,只能调用常成员函数
  19. const A *p = &a; // 指针变量,指向常对象
  20. const A &q = a; // 指向常对象的引用
  21. // 指针
  22. char greeting[] = "Hello";
  23. char* p1 = greeting; // 指针变量,指向字符数组变量
  24. const char* p2 = greeting; // 指针变量,指向字符数组常量(const 后面是 char,说明指向的字符(char)不可改变)
  25. char* const p3 = greeting; // 自身是常量的指针,指向字符数组变量(const 后面是 p3,说明 p3 指针自身不可改变)
  26. const char* const p4 = greeting; // 自身是常量的指针,指向字符数组常量
  27. }
  28. // 函数
  29. void function1(const int Var); // 传递过来的参数在函数内不可变
  30. void function2(const char* Var); // 参数指针所指内容为常量
  31. void function3(char* const Var); // 参数指针为常量
  32. void function4(const int& Var); // 引用参数在函数内为常量
  33. // 函数返回值
  34. const int function5(); // 返回一个常数
  35. const int* function6(); // 返回一个指向常量的指针变量,使用:const int *p = function6();
  36. int* const function7(); // 返回一个指向变量的常指针,使用:int* const p = function7();


  1. const Class_Name Object_name;
  2. //当一个函数被声明为const,它既可以被const的object调用,也可以被non-const的object调用
  3. //无论是声明成const的object还是声明成普通的object,都需要利用构造函数的初始化列表在初始化期间进行初始化
  4. //When a function is declared as const, it can be called on any type of object, const object as well as non-const objects.
  5. //Whenever an object is declared as const, it needs to be initialized at the time of declaration. however, the object initialization while declaring is possible only with the help of constructors.
  6. #include<iostream>
  7. using namespace std;
  8. class Sticker
  9. {
  10. private:
  11. double Perimeter() const;
  12. double Area() const;
  13. double Height:
  14. public:
  15. void GetDimensions();
  16. void Properties();
  17. }
  18. Sticker foo;
  19. foo.Area(); // ok
  20. foo.Properties(); // ok
  21. const Sticker bar;
  22. bar.Area(); // ok
  23. bar.Properties(); // error, bar is const
  24. //如果使用const进行实例化,那么实例化的object是不可以调用非const成员函数的,因为编译器会去检查是否有对const进行修改的行为。
  1. //A function becomes const when the const keyword is used in the function’s declaration. The idea of const functions is not to allow them to modify the object on which they are called.
  2. //It is recommended the practice to make as many functions const as possible so that accidental changes to objects are avoided.
  3. #include<iostream>
  4. using namespace std;
  5. class Test {
  6. int value;
  7. public:
  8. Test(int v = 0) {value = v;}
  9. // We get compiler error if we add a line like "value = 100;"
  10. // in this function.
  11. int getValue() const {return value;}
  12. };
  13. int main() {
  14. Test t(20);
  15. cout<<t.getValue();
  16. return 0;
  17. }
  18. //output
  19. 20

non-const的function只能被non-const的object所调用
When a function is declared as const, it can be called on any type of object. Non-const functions can only be called by non-const objects.
如下例子会产生编译错误

  1. #include<iostream>
  2. using namespace std;
  3. class Test {
  4. int value;
  5. public:
  6. Test(int v = 0) {value = v;}
  7. int getValue() {return value;}
  8. };
  9. int main() {
  10. const Test t;
  11. cout << t.getValue();
  12. return 0;
  13. }
  14. //Output
  15. Passing 'const Test' as 'this' argument of 'int
  16. Test::getValue()' discards qualifiers

再看下面这个例子

  1. filter_none
  2. edit
  3. play_arrow
  4. brightness_4
  5. // Demonstration of constant object,
  6. // show that constant object can only
  7. // call const member function
  8. #include<iostream>
  9. using namespace std;
  10. class Demo
  11. {
  12. int value;
  13. public:
  14. Demo(int v = 0) {value = v;}
  15. void showMessage()
  16. {
  17. cout<<"Hello World We are Tushar, "
  18. "Ramswarup, Nilesh and Subhash Inside"
  19. " showMessage() Function"<<endl;
  20. }
  21. void display()const
  22. {
  23. cout<<"Hello world I'm Rancho "
  24. "Baba Inside display() Function"<<endl;
  25. }
  26. };
  27. int main()
  28. {
  29. //Constant object are initialised at the time of declaration using constructor
  30. const Demo d1;
  31. //d1.showMessage();Error occurred if uncomment.
  32. d1.display();
  33. return(0);
  34. }
  35. //Output
  36. Hello world I'm Rancho Baba Inside display() Function

静态常量整型成员在class内部直接初始化

  1. #include <iostream>
  2. using namespace std;
  3. template <typename T>
  4. class testclass {
  5. public:
  6. static const int _datai = 5;
  7. static const long _datal = 3L;
  8. static const char _datac = 'c';
  9. };
  10. int main()
  11. {
  12. cout << testclass<int>::_datai << endl;
  13. cout << testclass<int>::_datal << endl;
  14. cout << testclass<int>::_datac << endl;
  15. }
  1. #include <iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int var1, var2;
  7. A()
  8. {
  9. var1 = 10;
  10. var2 = 20;
  11. }
  12. void fun() const // 不能在 const 修饰的成员函数中修改成员变量的值,除非该成员变量用 mutable 修饰
  13. {
  14. var1 = 100; // error: assignment of member 'A::var1' in read-only object
  15. }
  16. };
  17. int main()
  18. {
  19. A ex1;
  20. return 0;
  21. }

如果想达到一个类的成员函数不能修改类的成员变量,只需用 const 关键字来修饰该函数即可。

const区分技巧

区分各种const,只要从右往左读即可

  1. int const *: pointer to const int //等价于const int*
  2. int const* const: const pointer to const int
  3. int *const: const pointer to int
  4. int *const *:pointer to const pointer to int