问题:下面的定义是否合法?类里面是否可以存在const成员

  1. #include<stdio.h>
  2. class Test
  3. {
  4. private:
  5. const int ci;
  6. public:
  7. Test()
  8. {
  9. ci = 10;
  10. }
  11. int getCI()
  12. {
  13. return ci;
  14. }
  15. };
  16. int main()
  17. {
  18. Test t;
  19. printf("t.ci = %d\n",t.getCI());
  20. return 0;
  21. }
  22. 结果:
  23. test.cpp: In function ‘int main()’:
  24. test.cpp:16: error: structure ‘t’ with uninitialized const members
  25. //改进:使用构造函数初始化ci
  26. Test()
  27. {
  28. ci = 10;
  29. }
  30. //但是:
  31. test.cpp:8: error: uninitialized member ‘Test::ci’ with ‘const’ type ‘const int’
  32. test.cpp:10: error: assignment of read-only data-member ‘Test::ci’

1.初始化列表

c++中提供了初始化列表对成员变量进行初始化
语法规则:
calssName::ClassName():
m1(v1),m2(v1,v2),m3(v3)
{
//func;
}

改进以上例子:

  1. #include<stdio.h>
  2. class Test
  3. {
  4. private:
  5. const int ci;
  6. public:
  7. Test():ci(10)
  8. {
  9. //ci = 10;
  10. }
  11. int getCI()
  12. {
  13. return ci;
  14. }
  15. };
  16. int main()
  17. {
  18. Test t;
  19. printf("t.ci = %d\n",t.getCI());
  20. return 0;
  21. }

注意事项:
1.成员的初始化顺序与成员的声明顺序相同!!!!!!
2.成员的初始化顺序与初始化列表中的位置无关
3.初始化列表先于构造函数的函数体执行!!!

  1. //初始化列表的使用
  2. //初始化列表的使用
  3. #include<stdio.h>
  4. class Value
  5. {
  6. private:
  7. int mi;
  8. public:
  9. Value(int i)
  10. {
  11. printf("i = %d\n",i);
  12. mi = i;
  13. }
  14. int getMI()
  15. {
  16. return mi;
  17. }
  18. };
  19. class Test
  20. {
  21. private:
  22. Value m2;
  23. Value m3;
  24. Value m1;
  25. public:
  26. Test():m1(1),m2(2),m3(3)
  27. {
  28. printf("Test::Test()\n"); //观察初始化列表与构造函数的执行顺序
  29. }
  30. };
  31. int main()
  32. {
  33. Test t;
  34. //printf("t.ci = %d\n",t.getCI());
  35. return 0;
  36. }
  37. 证明成员的初始化顺序只与成员变量的声明顺序有关

2.类中的const成员

-类中的const成员会被分配空间
-类中const成员的本质是只读变量
-类中的const成员只能在初始化列表中指定初始值

编译器无法直接得到const成员的初始值,因此无法进入符号表成为真正意义上的常量。

  1. //初始化列表的使用
  2. #include<stdio.h>
  3. class Value
  4. {
  5. private:
  6. int mi;
  7. public:
  8. Value(int i)
  9. {
  10. printf("i = %d\n",i);
  11. mi = i;
  12. }
  13. int getMI()
  14. {
  15. return mi;
  16. }
  17. };
  18. class Test
  19. {
  20. private:
  21. Value m2;
  22. Value m3;
  23. Value m1;
  24. const int ci;
  25. public:
  26. Test():m1(1),m2(2),m3(3),ci(100)
  27. {
  28. printf("Test::Test()\n");
  29. }
  30. int getCI()
  31. {
  32. return ci;
  33. }
  34. void setCI(int v)
  35. {
  36. int* p = const_cast<int*>(&ci); //通过指针改变const变量的值,去掉const属性
  37. *p = v;
  38. }
  39. };
  40. int main()
  41. {
  42. Test t;
  43. printf("t.ci = %d\n",t.getCI());
  44. t.setCI(10);
  45. printf("t.ci = %d\n",t.getCI());
  46. return 0;
  47. }
  48. 结果
  49. i = 2
  50. i = 3
  51. i = 1
  52. Test::Test()
  53. t.ci = 100
  54. t.ci = 10
  55. 说明:const成员在类里面不能成为真正意义上的常量,只是只读变量

3.初始化和赋值不同

—-初始化:对正在创建的对象进行初值设置
—-赋值:对已经存在的对象进行值的设置

  1. int main()
  2. {
  3. int i = 0; //初始化
  4. i = 0; //赋值
  5. }

小结:

1.类中可以使用初始化列表对成员进行初始化
2.初始化列表先于函数体(构造函数)执行
3.类中可以定义const成员变量
4.const成员变量必须在初始化列表中指定初始值
5.const成员变量为只读变量,而不是真正意义上的常量