很简单概念的复习,对构造函数、拷贝构造函数和赋值函数:
1 #include <stdio.h>
2
3 class Bar
4 {
5 public:
6 Bar() {printf("ctor\n");}
7 Bar(Bar& b) {printf("copy ctor\n");}
8 Bar& operator = (const Bar & b) {printf("assignment =\n"); return *this;}
9 };
10
11 void foo(Bar b)
12 {
13 }
14
15 int main()
16 {
17 printf("Ctoring bar1\n");
18 Bar bar1;
19 printf("Ctoring bar2\n");
20 Bar bar2 = bar1;
21 printf("Ctoring bar3\n");
22 Bar bar3(bar1);
23 printf("Assigning bar3\n");
24 bar3 = bar2;
25 printf("Pass by value bar3\n");
26 foo(bar3);
27 try
28 {
29 try
30 {
31 printf("Throwing bar3\n");
32 throw bar3;
33 }
34 catch (Bar& bar)
35 {
36 printf("Rethrow bar3\n");
37 throw;
38 }
39 }
40 catch (…)
41 {
42 }
43 }
ctor
Ctoring bar2
copy ctor
Ctoring bar3
copy ctor
Assigning bar3
assignment =
Pass by value bar3
copy ctor
Throwing bar3
copy ctor
Rethrow bar3