您的位置:首页 > 编程语言 > C语言/C++

C++对象数组的创建

2014-07-27 23:59 302 查看
使用一维指针创建对象数组:

[cpp] view
plaincopy

//============================================================================

// 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;

}

结果:

[cpp] view
plaincopy

>>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

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

[cpp] view
plaincopy

//============================================================================

// 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;

}

结果:

[cpp] view
plaincopy

>>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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: