关于嵌套类的小实验

实验主要目的是明确在嵌套类中构造函数与析构函数的调用顺序与次数。

下面是代码,注意本次代码并没有显式的复制构造函数,并且主进程中也没有调用默认复制构造函数的操作。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 /****************************************************/
 6 class CPU
 7 {
 8 private:
 9     bool isrun=false;
10     int cpu_value;
11     int max_cpu_value;
12 public:
13     CPU(){cout<<"set a CPU object!"<<endl;};
14     ~CPU(){isrun=false;cout<<"delete a CPU object!"<<endl;};
15     CPU(int CpuValue,int MaxCpuValue):cpu_value(CpuValue),max_cpu_value(MaxCpuValue){cout<<"set a CPU object!"<<endl;};
16     void run(){isrun=true;};
17     void stop(){isrun=false;};
18     int get_cpu_value(){return cpu_value;};
19     void set_max_cpu_value(int v){max_cpu_value=v;};
20     int get_max_cpu_value(){return max_cpu_value;};
21 };
22 /****************************************************/
23 class RAM
24 {
25 private:
26     bool isLoaded=false;
27     unsigned ram_size;
28 public:
29     RAM(){cout<<"set a RAM object!"<<endl;};
30     ~RAM(){isLoaded=false;cout<<"delete a RAM object!"<<endl;};
31     RAM(unsigned MaxSize):ram_size(MaxSize){cout<<"set a RAM object!"<<endl;};
32     void RAM_load(){isLoaded=true;};
33     void RAM_unload(){isLoaded=false;};
34     unsigned get_ram_size(){return ram_size;};
35 };
36 /****************************************************/
37 class COMPUTER
38 {
39 private:
40     CPU main_cpu;
41     CPU* virtual_cpu=nullptr;
42     RAM main_ram;
43     RAM* virtual_ram=nullptr;
44     bool isrunning=false;
45 public:
46     COMPUTER(){cout<<"set a COMPUTER object!"<<endl;};
47     ~COMPUTER();
48     void RunComputer();
49     void StopComputer();
50     void set_virtual_cpu();
51     void set_virtual_ram();
52 };
53 /*--------------------------------------------------*/
54 
55 COMPUTER::~COMPUTER()
56 {
57     cout<<"delete a COMPUTER object!"<<endl;
58     if(virtual_cpu!=nullptr)delete virtual_cpu;
59     if(virtual_ram!=nullptr)delete virtual_ram;
60 }
61 
62 void COMPUTER::StopComputer()
63 {
64     main_cpu.stop();
65     main_ram.RAM_unload();
66     if(virtual_cpu!=nullptr)virtual_cpu->stop();
67     if(virtual_ram!=nullptr)virtual_ram->RAM_unload();
68 }
69 
70 void COMPUTER::set_virtual_ram()
71 {
72     virtual_ram=new RAM(main_ram.get_ram_size());
73     virtual_ram->RAM_load();
74 }
75 
76 void COMPUTER::set_virtual_cpu()
77 {
78     virtual_cpu=new CPU(0,(main_cpu.get_max_cpu_value())/2);
79     virtual_cpu->run();
80 }
81 
82 void COMPUTER::RunComputer()
83 {
84     main_cpu.run();
85     main_ram.RAM_load();
86     isrunning=true;
87 }
88 /*--------------------------------------------------*/
89 /****************************************************/
90 
91 int main()
92 {
93     COMPUTER mycomputer;
94     mycomputer.set_virtual_cpu();
95     mycomputer.set_virtual_ram();
96     mycomputer.RunComputer();
97     mycomputer.StopComputer();
98     return 0;
99 }

运行结果如下:

set a CPU object!
set a RAM object!
set a COMPUTER object!
set a CPU object!
set a RAM object!
delete a COMPUTER object!
delete a CPU object!
delete a RAM object!
delete a RAM object!
delete a CPU object!

Process returned 0 (0x0)   execution time : 0.002 s
Press ENTER to continue.

各个类型的构造与析构次数相对应。

如果有复制构造函数的使用,则在一下情形下会发生构造与析构,以下情形也正是复制构造函数的调用的三种情形:在一个对象以值传递的方式传入函数体时;一个对象以值传递的方式从函数返回时;一个对象用于给另外一个对象进行初始化(常称为赋值初始化)时被自动调用。

posted @ 2020-02-28 10:01  SavenNeer  阅读(140)  评论(0编辑  收藏  举报