歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++ 類構造函數和析構函數

C++ 類構造函數和析構函數

日期:2017/3/1 9:14:34   编辑:Linux編程

C++ 類構造函數和析構函數
1、構造函數:構造函數用於對對象的數據進行初始化,構造函數的和一般的方法(函數)有一些不同
他的名字必須是類的名字,不能帶返回值。一般來說即使你不建立構造函數,也會
為你建立默認的構造函數,但是默認的構造函數是什麼都不干的。如:
stu::stu(void){}
2、析構函數:析構函數用於對對象的內存進行回收,(如用malloc和new分配的內存空間)析構函數在
對象消亡的時候會被自動調用,而不需要你手動調用,名稱為類名字前面加上~。一樣
析構函數不能帶返回值,並且析構函數沒有參數,同樣如果你設置析構函數,也會
為你默認建立析構函數,但是默認也是什麼都不做的。

另外需要注意的是:
1、構造函數可以有3種調用方法
--stu yl(1,"yanlei") 隱含調用,並且賦值
--stu yl; 隱含調用,可能函數重載了
--stu yl = stu(1,"yanlei") 顯示調用,並且賦值
注意調用stu yl(void)是不可以的這是一個返回stu類的函數原型
(C++11引入了其他的初始化方式這裡不討論)
2、構造函數,不能帶返回值。析構函數,不能帶參數和返回值
3、對象初始化後可以使用構造函數重新賦值如下:
stu yl = stu(1,"yanlei");
yl = stu(1,"gaopeng");
但是此時使用的方法是建立一塊臨時空間,然後復制對象數據到對象,然後
釋放,所以需要調用一次析構函數。
4、構造函數他的名字必須是類的名字,析構函數類名字前面加上~ 如:
stu::stu(void);
stu::~stu(void);
來看一段代碼:

  1. ::::::::::::::
  2. ct1.h
  3. ::::::::::::::
  4. /*************************************************************************
  5. > File Name: ct1.h
  6. > Author: gaopeng
  7. > Mail: [email protected]
  8. > Created Time: Mon 13 Jun 2016 01:54:32 AM CST
  9. ************************************************************************/
  10. #include<iostream>
  11. using namespace std;
  12. typedef struct mem
  13. {
  14. int t_sorce_;
  15. const char *name_;
  16. } MEM;
  17. class stu
  18. {
  19. private:
  20. int id_;
  21. MEM st_;
  22. public:
  23. stu(int id,const char *name);//構造函數
  24. stu(void);//構造函數
  25. int set_src(int id,int a,int b,const char *name);
  26. int sh_src(void);
  27. const stu* check(const stu* info) const;
  28. ~stu(void); //析構函數
  29. };
  30. ::::::::::::::
  31. fu.cpp
  32. ::::::::::::::
  33. /*************************************************************************
  34. > File Name: fu.cpp
  35. > Author: gaopeng
  36. > Mail: [email protected]
  37. > Created Time: Mon 13 Jun 2016 02:02:26 AM CST
  38. ************************************************************************/
  39. #include<iostream>
  40. #include "ct1.h"
  41. using namespace std;
  42. //構造函數使用函數重載
  43. stu::stu(int id,const char *name) //構造函數,不能帶返回值
  44. {
  45. id_ = id;
  46. st_.name_ = name;
  47. st_.t_sorce_ = 0;
  48. }
  49. stu::stu(void) //構造函數,不能帶返回值
  50. {
  51. static char name[20] = "gaopeng";
  52. st_.name_ = name;
  53. id_ = 0;
  54. st_.t_sorce_ = 0;
  55. }
  56. stu::~stu(void) //析構函數,不能帶參數和返回值
  57. {
  58. cout << "call destructor for name:" << st_.name_ <<endl;
  59. }
  60. int stu::set_src(int id,int a,int b,const char *name)
  61. {
  62. id_ = id;
  63. st_.t_sorce_ = a+b;
  64. st_.name_ = name;
  65. return 0;
  66. }
  67. int stu::sh_src(void)
  68. {
  69. cout << id_ <<endl;
  70. cout << st_.t_sorce_ <<endl;
  71. cout << st_.name_ <<endl;
  72. return 0;
  73. }
  74. const stu* stu::check(const stu* info) const
  75. {
  76. if(info->id_ > id_ )
  77. {
  78. return info;
  79. }
  80. else
  81. {
  82. return this; // this 指針
  83. }
  84. }
  85. ::::::::::::::
  86. main.cpp
  87. ::::::::::::::
  88. /*************************************************************************
  89. > File Name: main.cpp
  90. > Author: gaopeng
  91. > Mail: [email protected]
  92. > Created Time: Mon 13 Jun 2016 02:18:10 AM CST
  93. ************************************************************************/
  94. #include<iostream>
  95. #include"ct1.h"
  96. using namespace std;
  97. int main(void)
  98. {
  99. {
  100. char a[20] = "yanlei";
  101. stu yl(1,a); //stu::stu(int id,const char *name),方法1進行初始化,顯示初始化
  102. yl.sh_src();
  103. stu gp;// stu::stu(void) ,隱含初始化調用了重載函數stu()
  104. gp.sh_src();
  105. gp.set_src(2,10,20,"gaopeng1");
  106. gp.sh_src();
  107. stu gp2 = stu(3,"gaopeng2");//方法2進行初始化,方法2初始化可以有2個意思,意思1:初始化 意思2:如果已經初始化再次調用建立臨時內容然後COPY到gp2的方式,並且釋放臨時
  108. 內存調用析構函數
  109. gp2 = stu(3,"gaopeng3"); //再次重構類數據,通過建立臨時內容然後COPY到gp2的方式,並且釋放臨時內存,所以調用了析構函數
  110. gp2 = gp;
  111. gp2.sh_src();
  112. stu gp3;
  113. cout << "gp3 resource:"<<endl;
  114. gp3.sh_src();
  115. gp3 = *(gp3.check(&gp2));
  116. cout << "gp3 after check:"<<endl;
  117. gp3.sh_src();
  118. }
  119. }

其中都有詳細的解析,程序執行結果如下:
1
0
yanlei
0
0
gaopeng
2
30
gaopeng1
call destructor for name:gaopeng3
2
30
gaopeng1
gp3 resource:
0
0
gaopeng
gp3 after check:
2
30
gaopeng1
call destructor for name:gaopeng1
call destructor for name:gaopeng1
call destructor for name:gaopeng1
call destructor for name:yanlei

注意call destructor for name:gaopeng3就是臨時空間釋放調用的析構函數
並且注意this指針用法

Copyright © Linux教程網 All Rights Reserved