Use a one-dimensional pointer to create an object array:
// ================================================ ==============================================
// 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;
}
Result:
> 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
Use a two-dimensional pointer to create a dynamic array:
// ================================================ ==============================================
// 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; // This statement does not seem to work.
Cout <"nextStudentID:" <nextStudentID <endl;
Return 0;
}
Result:
> 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
Differences between the two:
One-dimensional pointers apply for and create three objects when creating pointers.
A two-dimensional pointer creates only one pointer array (3*4 bytes) when creating a pointer. The elements in the array are used to store the addresses of three objects, each object address is created by new. Therefore, the corresponding delete function should be delete p [j], rather than delete [] p.
From Study Notes of sg131971