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。
[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。
相关文章推荐
- c++动态的创建对象数组
- 对C++中用vector创建数组对象的认识
- C++对象创建(二)
- C++学习:在C++中创建持久对象
- C++创建对象的两种方法
- 转:C++对象的创建
- 一个新的对象创建机制的思考--C++下实现类似于Java的对象管理机制及反射
- C++中的数组和局部静态对象
- 在C++中创建持久对象
- C++ 创建对象
- C++ 创建对象
- C++创建对象的两种方法
- C++ RTTI: 2个宏实现“根据对象名创建对象”
- 仿MFC实现c++按名动态创建对象之续(瘦身以及解决其在dll中使用的问题)
- 仿MFC实现c++的按名动态创建对象功能
- C++只能允许类创建一个对象的做法
- C++应用程序中创建持久对象
- C++ 用new 动态创建多维数组
- 读取配置文件动态创建对象[C++]
- C++对象创建过程解密