C++对象数组的创建

来源:互联网 发布:js模块化规范 编辑:程序博客网 时间:2024/05/16 14:42

使用一维指针创建对象数组:

[cpp] view plaincopy
  1. //============================================================================  
  2. // Name        : main.cpp  
  3. // Author      : ShiGuang  
  4. // Version     :  
  5. // Copyright   : sg131971@qq.com  
  6. // Description : Hello World in C++, Ansi-style  
  7. //============================================================================  
  8.   
  9. #include <iostream>  
  10. #include <string>  
  11. using namespace std;  
  12.   
  13. int nextStudentID = 1;  
  14. class StudentID  
  15. {  
  16. public:  
  17.     StudentID()  
  18.     {  
  19.         cout << "StudentID()" << endl;  
  20.         value = nextStudentID++;  
  21.         cout << "value:" << value << endl;  
  22.     }  
  23.     ~StudentID()  
  24.     {  
  25.         --nextStudentID;  
  26.         cout << "~StudentID()" << endl;  
  27.     }  
  28. protected:  
  29.     int value;  
  30. };  
  31.   
  32. class Student  
  33. {  
  34. public:  
  35.     Student(string pName = "noName")  
  36.     {  
  37.         cout << "Student()" << endl;  
  38.         name = pName;  
  39.         cout << "name:" << name << endl;  
  40.     }  
  41.     ~Student()  
  42.     {  
  43.         cout << "~Student()" << endl;  
  44.     }  
  45. protected:  
  46.     string name;  
  47.     StudentID id;  
  48. };  
  49.   
  50. int main(int argc, char **argv)  
  51. {  
  52.     int i;  
  53.     cin >> i;  
  54.     Student *p = new Student [i];  
  55.     delete[] p;  
  56.     cout << "nextStudentID:" << nextStudentID << endl;  
  57.     return 0;  
  58. }  

结果:

[cpp] view plaincopy
  1. >>3  
  2. StudentID()  
  3. value:1  
  4. Student()  
  5. name:noName  
  6. StudentID()  
  7. value:2  
  8. Student()  
  9. name:noName  
  10. StudentID()  
  11. value:3  
  12. Student()  
  13. name:noName  
  14. ~Student()  
  15. ~StudentID()  
  16. ~Student()  
  17. ~StudentID()  
  18. ~Student()  
  19. ~StudentID()  
  20. nextStudentID:1  

使用二维指针创建动态数组:

[cpp] view plaincopy
  1. //============================================================================  
  2. // Name        : main.cpp  
  3. // Author      : ShiGuang  
  4. // Version     :  
  5. // Copyright   : sg131971@qq.com  
  6. // Description : Hello World in C++, Ansi-style  
  7. //============================================================================  
  8.   
  9. #include <iostream>  
  10. #include <string>  
  11. using namespace std;  
  12.   
  13. int nextStudentID = 1;  
  14. class StudentID  
  15. {  
  16. public:  
  17.     StudentID()  
  18.     {  
  19.         cout << "StudentID()" << endl;  
  20.         value = nextStudentID++;  
  21.         cout << "value:" << value << endl;  
  22.     }  
  23.     ~StudentID()  
  24.     {  
  25.         --nextStudentID;  
  26.         cout << "~StudentID()" << endl;  
  27.     }  
  28. protected:  
  29.     int value;  
  30. };  
  31.   
  32. class Student  
  33. {  
  34. public:  
  35.     Student(string pName = "noName")  
  36.     {  
  37.         cout << "Student()" << endl;  
  38.         name = pName;  
  39.         cout << "name:" << name << endl;  
  40.     }  
  41.     ~Student()  
  42.     {  
  43.         cout << "~Student()" << endl;  
  44.     }  
  45. protected:  
  46.     string name;  
  47.     StudentID id;  
  48. };  
  49.   
  50. int main(int argc, char **argv)  
  51. {  
  52.     int i, j;  
  53.     string temp;  
  54.     cin >> i;  
  55.     Student **p = new Student *[i];  
  56.     for (j = 0; j < i; j++)  
  57.     {  
  58.         cout << "j:" << j << endl;  
  59.         cin >> temp;  
  60.         p[j] = new Student(temp);  
  61.         cout << "nextStudentID:" << nextStudentID << endl;  
  62.     }  
  63.     for (j = i - 1; j >= 0; j--)  
  64.         delete p[j];  
  65. //  delete[] p; // 这句话好像没作用  
  66.     cout << "nextStudentID:" << nextStudentID << endl;  
  67.     return 0;  
  68. }  
结果:

[cpp] view plaincopy
  1. >>3  
  2. j:0  
  3. >>shiguang1  
  4. StudentID()  
  5. value:1  
  6. Student()  
  7. name:shiguang1  
  8. nextStudentID:2  
  9. j:1  
  10. >>shiguang2  
  11. StudentID()  
  12. value:2  
  13. Student()  
  14. name:shiguang2  
  15. nextStudentID:3  
  16. j:2  
  17. >>shiguang3  
  18. StudentID()  
  19. value:3  
  20. Student()  
  21. name:shiguang3  
  22. nextStudentID:4  
  23. ~Student()  
  24. ~StudentID()  
  25. ~Student()  
  26. ~StudentID()  
  27. ~Student()  
  28. ~StudentID()  
  29. nextStudentID:1  

两者的区别:

一维指针在建立指针的时候,申请并创建了三个对象。

二维指针在建立指针的时候,只创建了一个指针数组(即3*4Byte),数组里面的元素用来存放三个对象的地址,各个对象地址在后面由new创建获得。所以对应的delete函数应该是delete p[j];而不是delete[] p。


0 0