1.#
点击查看考点
生命期
1#include <iostream>
2
3using namespace std;
4
5class Guest {
6 public:
7 Guest(char const* Name = "NoName") {
8 pName = Name;
9 cout << "Construct Guest of " << pName << endl;
10 }
11 ~Guest() {
12 cout << "Destruct Guest of " << pName << endl;
13 }
14
15 private:
16 char const* pName;
17};
18
19class Host {
20 public:
21 Host(Guest const& gs) : hs(gs) {
22 cout << "Construct...\n";
23 }
24
25 private:
26 Guest hs;
27};
28
29Guest g1("g1");
30Host h1(g1);
31
32int main() {
33 cout << "in the main function\n";
34 {
35 cout << "in the block\n";
36 static Host h2(h1);
37 }
38 cout << "end the main function" << endl;
39}
点击查看答案
1Construct Guest of g1
2Construct...
3in the main function
4in the block
5end the main function
6Destruct Guest of g1
7Destruct Guest of g1
8Destruct Guest of g1