1.构造函数详解

(1)带有参数的构造函数

—-构造函数可以根据需要定义参数
—-一个类中可以存在多个重载的构造函数
—-构造函数的重载遵循c++重载的规则

  1. class Test
  2. {
  3. public:
  4. Test(int V)
  5. {
  6. //use v to initialize members
  7. }
  8. };

(2)对象定义与对象声明的不同

—对象定义:申请对象的空间并调用构造函数
—对象声明:告诉编译器存在这样一个对象

  1. Test t;//定义对象并调用构造函数
  2. int main()
  3. {
  4. extern Test t; //告诉编译器存在名为t的Test 对象
  5. return 0;
  6. }

(3)构造函数的自动调用

  1. class Test
  2. {
  3. public:
  4. Test();
  5. Test(int v);
  6. };
  7. int main()
  8. {
  9. Test t; //调用test
  10. Test t1(1); //调用Test(int v)
  11. Test t2 = 1; //调用Test(int v)
  12. return 0;
  13. }

(4)实例分析

  1. #include<stdio.h>
  2. class Test
  3. {
  4. public:
  5. Test()
  6. {
  7. printf("Test()\n");
  8. }
  9. Test(int v)
  10. {
  11. printf("Test(int v),v = %d\n",v);
  12. }
  13. };
  14. int main()
  15. {
  16. Test t; //调用test
  17. Test t1(1); //调用Test(int v)
  18. Test t2 = 2; //调用Test(int v),初始化
  19. //思考项
  20. //t2 = 12; //赋值不调用构造函数
  21. int i(100); //初始化
  22. printf("i = %d\n",i);
  23. return 0;
  24. }

输出:
Test()
Test(int v),v = 1
Test(int v),v = 2

(5)构造函数的调用

—-一些特殊情况下,构造函数在对象定义时被自动调用
—-一些特殊情况下,需要手工调用构造函数
手工调用构造函数

  1. #include<stdio.h>
  2. class Test
  3. {
  4. private:
  5. int m_value;
  6. public:
  7. Test()
  8. {
  9. printf("Test()\n");
  10. m_value = 0;
  11. }
  12. Test(int v)
  13. {
  14. printf("Test(int v),v = %d\n",v);
  15. m_value = v;
  16. }
  17. int getValue()
  18. {
  19. return m_value;
  20. }
  21. };
  22. int main()
  23. {
  24. Test ta[3] = {Test(),Test(1),Test(2)}; //调用test
  25. for (int i = 0; i < 3; i++)
  26. {
  27. printf("ta[%d].getValue() = %d\n",i,ta[i].getValue());
  28. }
  29. //另一种初始化方式:使用构造函数来进行初始化
  30. Test t = Test(100);
  31. printf("t.getValue() = %d\n", t.getValue());
  32. return 0;
  33. }

2.实例分析

Slide8.PNG

  1. #pragma once
  2. //IntArray.h
  3. #ifndef _INTARRAY_H_
  4. #define _INTARRAY_H_
  5. class IntArray
  6. {
  7. private:
  8. int m_length;
  9. int* m_pointer;
  10. public:
  11. IntArray(int len);
  12. int length();
  13. bool get(int index, int& value);
  14. bool set(int index, int value);
  15. void free();
  16. };
  17. #endif
  18. //IntArray.c
  19. #include"IntArray.h"
  20. IntArray::IntArray(int len)
  21. {
  22. m_pointer = new int(len); //需要额外的free函数进行释放
  23. for (int i = 0; i < len; i++)
  24. {
  25. m_pointer[i] = 0;
  26. }
  27. m_length = len;
  28. }
  29. int IntArray::length()
  30. {
  31. return m_length;
  32. }
  33. bool IntArray::get(int index, int& value)
  34. {
  35. bool ret = (0 <= index) && (index < m_length);
  36. if (ret)
  37. {
  38. value = m_pointer[index];
  39. }
  40. return ret;
  41. }
  42. bool IntArray::set(int index, int value)
  43. {
  44. bool ret = (0 <= index) && (index < m_length);
  45. if (ret)
  46. {
  47. m_pointer[index] = value;
  48. }
  49. return ret;
  50. }
  51. void IntArray::free()
  52. {
  53. delete[] m_pointer;
  54. }
  55. #include<stdio.h>
  56. #include"./test/IntArray.h"
  57. int main()
  58. {
  59. IntArray a(5); //定义一个对象a,数组大小为5
  60. for (int i=0; i<a.length(); i++)
  61. {
  62. a.set(i, i + 1);
  63. }
  64. for (int i = 0; i < a.length(); i++)
  65. {
  66. int value = 0;
  67. if (a.get(i, value))
  68. {
  69. printf("a[%d] = %d\n", i, value);
  70. }
  71. }
  72. a.free();
  73. return 0;
  74. }

小结

构造函数可以根据需要定义参数
构造函数之间可以存在重载关系
构造函数遵循c++中重载函数的规则
对象定义时会触发构造函数的调用
在一些情况下可以手动调用构造函数