C++对象数组的创建
来源:互联网 发布:js模块化规范 编辑:程序博客网 时间:2024/05/16 14:42
使用一维指针创建对象数组:
- //============================================================================
- // Name : main.cpp
- // Author : ShiGuang
- // Version :
- // Copyright : sg131971@qq.com
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <string>
- using namespace std;
- int nextStudentID = 1;
- class StudentID
- {
- public:
- StudentID()
- {
- cout << "StudentID()" << endl;
- value = nextStudentID++;
- cout << "value:" << value << endl;
- }
- ~StudentID()
- {
- --nextStudentID;
- cout << "~StudentID()" << endl;
- }
- protected:
- int value;
- };
- class Student
- {
- public:
- Student(string pName = "noName")
- {
- cout << "Student()" << endl;
- name = pName;
- cout << "name:" << name << endl;
- }
- ~Student()
- {
- cout << "~Student()" << endl;
- }
- protected:
- string name;
- StudentID id;
- };
- int main(int argc, char **argv)
- {
- int i;
- cin >> i;
- Student *p = new Student [i];
- delete[] p;
- cout << "nextStudentID:" << nextStudentID << endl;
- return 0;
- }
结果:
- >>3
- StudentID()
- value:1
- Student()
- name:noName
- StudentID()
- value:2
- Student()
- name:noName
- StudentID()
- value:3
- Student()
- name:noName
- ~Student()
- ~StudentID()
- ~Student()
- ~StudentID()
- ~Student()
- ~StudentID()
- nextStudentID:1
使用二维指针创建动态数组:
- //============================================================================
- // Name : main.cpp
- // Author : ShiGuang
- // Version :
- // Copyright : sg131971@qq.com
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <string>
- using namespace std;
- int nextStudentID = 1;
- class StudentID
- {
- public:
- StudentID()
- {
- cout << "StudentID()" << endl;
- value = nextStudentID++;
- cout << "value:" << value << endl;
- }
- ~StudentID()
- {
- --nextStudentID;
- cout << "~StudentID()" << endl;
- }
- protected:
- int value;
- };
- class Student
- {
- public:
- Student(string pName = "noName")
- {
- cout << "Student()" << endl;
- name = pName;
- cout << "name:" << name << endl;
- }
- ~Student()
- {
- cout << "~Student()" << endl;
- }
- protected:
- string name;
- StudentID id;
- };
- int main(int argc, char **argv)
- {
- int i, j;
- string temp;
- cin >> i;
- Student **p = new Student *[i];
- for (j = 0; j < i; j++)
- {
- cout << "j:" << j << endl;
- cin >> temp;
- p[j] = new Student(temp);
- cout << "nextStudentID:" << nextStudentID << endl;
- }
- for (j = i - 1; j >= 0; j--)
- delete p[j];
- // delete[] p; // 这句话好像没作用
- cout << "nextStudentID:" << nextStudentID << endl;
- return 0;
- }
- >>3
- j:0
- >>shiguang1
- StudentID()
- value:1
- Student()
- name:shiguang1
- nextStudentID:2
- j:1
- >>shiguang2
- StudentID()
- value:2
- Student()
- name:shiguang2
- nextStudentID:3
- j:2
- >>shiguang3
- StudentID()
- value:3
- Student()
- name:shiguang3
- nextStudentID:4
- ~Student()
- ~StudentID()
- ~Student()
- ~StudentID()
- ~Student()
- ~StudentID()
- nextStudentID:1
两者的区别:
一维指针在建立指针的时候,申请并创建了三个对象。
二维指针在建立指针的时候,只创建了一个指针数组(即3*4Byte),数组里面的元素用来存放三个对象的地址,各个对象地址在后面由new创建获得。所以对应的delete函数应该是delete p[j];而不是delete[] p。
0 0
- C++对象数组的创建
- 如何创建对象的数组
- object-c对象的创建
- 普通对象(不包括数组对象、Class对象)的创建
- 对象数组的创建与删除
- 关于JAVA类对象数组的创建
- JavaScript - 用对象的方式创建数组
- Java String数组对象的创建测试
- 创建自定义类的对象数组
- 创建数组对象
- java创建对象数组
- js创建数组,创建对象
- Java中对象数组的创建【VS】c++中对象数组的创建
- new创建对象数组 比较 new单个对象的优点
- js中的数组与对象的创建以及json对象
- C/C++动态数组的创建
- C/C++动态数组的创建
- C语言动态数组的创建
- POJ 2155 Matrix (二维树状数组)
- Redis介绍及常用命令
- 【iOS】多线程NSOperation
- HDU 1234 开门人和关门人
- 有关g++的-rdynamic选项
- C++对象数组的创建
- gcc -rdynamic参数解释
- tts和字符集的关系--要求源和目的端的数据库字符集必须一样,国家字符集必须一样。
- 记事本中写c/c++程序在Windows下执行
- Java容器学习之Map
- gcc/g++ 静态动态库 混链接.
- mac 安装jdk和eclipse环境
- 使用Maven为一个项目生成多个Jar包
- UVA 10608并查集