h-----------------------------
#include "newandmalloc.h"
#include <iostream>
using namespace std;
newAndMalloc::newAndMalloc()
{
cout<<"construct newAndMalloc..."<<endl;
}
newAndMalloc::~newAndMalloc()
{
cout<<"destruct newAndMalloc..."<<endl;
deletePInt();
deletePPInt();
deletePPPInt();
}
void newAndMalloc::newPInt()
{
cout<<"new m_pInt..."<<endl;
m_pInt = new int[m_size]; //开辟空间
for (int i = 0; i < m_size; i++) //赋值
{
m_pInt[i] = m_size - i;
}
}
void newAndMalloc::newPPInt()
{
cout<<"new m_ppInt..."<<endl;
m_ppInt = new int*[m_size]; //开辟一维的空间
for (int i = 0; i < m_size; i++)
{
m_ppInt[i] = new int[m_size]; //开辟二维的控件
}
for (int i = 0; i < m_size; i++) //赋值
{
for (int j = 0; j < m_size; j++)
{
m_ppInt[i][j] = i + j;
}
}
}
void newAndMalloc::newPPPInt()
{
cout<<"new m_pppInt..."<<endl;
m_pppInt = new int**[m_size]; //开辟一维的空间
for(int i = 0; i < m_size; i++)
{
m_pppInt[i] = new int*[m_size]; //开辟二维的空间
for (int j = 0; j < m_size; j++)
{
m_pppInt[i][j] = new int[m_size]; //开辟三维的空间
}
}
for (int i = 0; i < m_size; i++) //赋值
{
for (int j = 0; j < m_size; j++)
{
for (int h = 0; h < m_size; h++)
{
m_pppInt[i][j][h] = i + j + h;
}
}
}
}
void newAndMalloc::printPInt()
{
cout<<"print m_pInt..."<<endl;
for (int i = 0; i < m_size; i++)
{
cout<<m_pInt[i]<<" ";
}
cout<<endl;
}
void newAndMalloc::printPPInt()
{
cout<<"print m_ppInt..."<<endl;
for (int i = 0; i < m_size; i++)
{
for (int j = 0; j < m_size; j++)
{
cout<<m_ppInt[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
void newAndMalloc::printPPPInt()
{
cout<<"print m_pppInt..."<<endl;
for (int i = 0; i < m_size; i++)
{
for (int j = 0; j < m_size; j++)
{
for (int h = 0; h < m_size; h++)
{
cout<<m_pppInt[i][j][h]<<" ";
}
cout<<endl;
}
cout<<endl;
}
cout<<endl;
}
void newAndMalloc::deletePInt()
{
cout<<"delete m_pInt..."<<endl;
delete[] m_pInt;
}
void newAndMalloc::deletePPInt()
{
cout<<"delete m_ppInt..."<<endl;
for (int i = 0; i < m_size; i++)
{
delete[] m_ppInt[i];
}
delete[] m_ppInt;
}
void newAndMalloc::deletePPPInt()
{
cout<<"delete m_pppInt..."<<endl;
for (int i = 0; i < m_size; i++)
{
for (int j = 0; j < m_size; j++)
{
delete[] m_pppInt[i][j];
}
delete[] m_pppInt[i];
}
delete[] m_pppInt;
}
testClass::testClass()
{
cout<<"construct testClass..."<<endl;
}
testClass::~testClass()
{
cout<<"destruct testClass..."<<endl;
}
cpp--------------------------------
#include "newandmalloc.h"
#include <iostream>
using namespace std;
newAndMalloc::newAndMalloc()
{
cout<<"construct newAndMalloc..."<<endl;
}
newAndMalloc::~newAndMalloc()
{
cout<<"destruct newAndMalloc..."<<endl;
deletePInt();
deletePPInt();
deletePPPInt();
}
void newAndMalloc::newPInt()
{
cout<<"new m_pInt..."<<endl;
m_pInt = new int[m_size]; //开辟空间
for (int i = 0; i < m_size; i++) //赋值
{
m_pInt[i] = m_size - i;
}
}
void newAndMalloc::newPPInt()
{
cout<<"new m_ppInt..."<<endl;
m_ppInt = new int*[m_size]; //开辟一维的空间
for (int i = 0; i < m_size; i++)
{
m_ppInt[i] = new int[m_size]; //开辟二维的控件
}
for (int i = 0; i < m_size; i++) //赋值
{
for (int j = 0; j < m_size; j++)
{
m_ppInt[i][j] = i + j;
}
}
}
void newAndMalloc::newPPPInt()
{
cout<<"new m_pppInt..."<<endl;
m_pppInt = new int**[m_size]; //开辟一维的空间
for(int i = 0; i < m_size; i++)
{
m_pppInt[i] = new int*[m_size]; //开辟二维的空间
for (int j = 0; j < m_size; j++)
{
m_pppInt[i][j] = new int[m_size]; //开辟三维的空间
}
}
for (int i = 0; i < m_size; i++) //赋值
{
for (int j = 0; j < m_size; j++)
{
for (int h = 0; h < m_size; h++)
{
m_pppInt[i][j][h] = i + j + h;
}
}
}
}
void newAndMalloc::printPInt()
{
cout<<"print m_pInt..."<<endl;
for (int i = 0; i < m_size; i++)
{
cout<<m_pInt[i]<<" ";
}
cout<<endl;
}
void newAndMalloc::printPPInt()
{
cout<<"print m_ppInt..."<<endl;
for (int i = 0; i < m_size; i++)
{
for (int j = 0; j < m_size; j++)
{
cout<<m_ppInt[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
void newAndMalloc::printPPPInt()
{
cout<<"print m_pppInt..."<<endl;
for (int i = 0; i < m_size; i++)
{
for (int j = 0; j < m_size; j++)
{
for (int h = 0; h < m_size; h++)
{
cout<<m_pppInt[i][j][h]<<" ";
}
cout<<endl;
}
cout<<endl;
}
cout<<endl;
}
void newAndMalloc::deletePInt()
{
cout<<"delete m_pInt..."<<endl;
delete[] m_pInt;
}
void newAndMalloc::deletePPInt()
{
cout<<"delete m_ppInt..."<<endl;
for (int i = 0; i < m_size; i++)
{
delete[] m_ppInt[i];
}
delete[] m_ppInt;
}
void newAndMalloc::deletePPPInt()
{
cout<<"delete m_pppInt..."<<endl;
for (int i = 0; i < m_size; i++)
{
for (int j = 0; j < m_size; j++)
{
delete[] m_pppInt[i][j];
}
delete[] m_pppInt[i];
}
delete[] m_pppInt;
}
testClass::testClass()
{
cout<<"construct testClass..."<<endl;
}
testClass::~testClass()
{
cout<<"destruct testClass..."<<endl;
}
main-------------------------------
#include <iostream>
#include <stdio.h>
#include "newandmalloc.h"
int main(int argc, char *argv[])
{
newAndMalloc *nam = nullptr;
nam = new newAndMalloc();
nam->newPInt();
nam->newPPInt();
nam->newPPPInt();
nam->printPInt();
nam->printPPInt();
nam->printPPPInt();
delete nam;
return 0;
}
打印输出------------------------
construct newAndMalloc...
new m_pInt...
new m_ppInt...
new m_pppInt...
print m_pInt...
10 9 8 7 6 5 4 3 2 1
print m_ppInt...
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
print m_pppInt...
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19 20
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19 20
12 13 14 15 16 17 18 19 20 21
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19 20
12 13 14 15 16 17 18 19 20 21
13 14 15 16 17 18 19 20 21 22
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19 20
12 13 14 15 16 17 18 19 20 21
13 14 15 16 17 18 19 20 21 22
14 15 16 17 18 19 20 21 22 23
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19 20
12 13 14 15 16 17 18 19 20 21
13 14 15 16 17 18 19 20 21 22
14 15 16 17 18 19 20 21 22 23
15 16 17 18 19 20 21 22 23 24
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19 20
12 13 14 15 16 17 18 19 20 21
13 14 15 16 17 18 19 20 21 22
14 15 16 17 18 19 20 21 22 23
15 16 17 18 19 20 21 22 23 24
16 17 18 19 20 21 22 23 24 25
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19 20
12 13 14 15 16 17 18 19 20 21
13 14 15 16 17 18 19 20 21 22
14 15 16 17 18 19 20 21 22 23
15 16 17 18 19 20 21 22 23 24
16 17 18 19 20 21 22 23 24 25
17 18 19 20 21 22 23 24 25 26
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
11 12 13 14 15 16 17 18 19 20
12 13 14 15 16 17 18 19 20 21
13 14 15 16 17 18 19 20 21 22
14 15 16 17 18 19 20 21 22 23
15 16 17 18 19 20 21 22 23 24
16 17 18 19 20 21 22 23 24 25
17 18 19 20 21 22 23 24 25 26
18 19 20 21 22 23 24 25 26 27
destruct newAndMalloc...
delete m_pInt...
delete m_ppInt...
delete m_pppInt...