临时对象专题

学习临时对象时的相关代码:

例1:

 1 /*
 2  *第一种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B Func(B b)
41 {
42     return b;
43 }
44 
45 void main()
46 {
47     B t1 = Func(5);
48 }
49 
50 //Out  put   
51 /*
52 Construct  By: 0x0012FF20  5  //隐式类型转换构建临时对象
53 Copy  Construct  0x0012FF7C   0x0012FF20  //拷贝构造函数进行传参(b = 临时对象)
54 Destructed   0x0012FF20  5   //摧毁临时对象
55 Destructed   0x0012FF7C  5   //摧毁对象t1
56 */ 

例2:

 1 /*
 2  *第二种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B Func(B b)
41 {
42     return b;
43 }
44 
45 void main()
46 {
47     B t1;
48     t1 = Func(5);
49 }
50 
51 //Out  put 
52 /*
53 Default  Construct:  0012FF58   //默认构造创建t1
54 Construct  By: 0012FE5C  5      //隐式类型转换创建临时对象
55 Copy  Construct  0012FE8C   0012FE5C  //拷贝构造函数传参(b=临时对象)
56 Destructed   0012FE5C  5        //摧毁临时对象
57 operator=   0012FF58   0012FE8C//赋值函数进行赋值(t1=b)
58 Destructed   0012FE8C  5    //摧毁对象b
59 Destructed   0012FF58  5    //摧毁对象t1
60 */

例3:

 1 /*
 2  *第三种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B Func(B b)
41 {
42     return b;
43 }
44 
45 void main()
46 {
47     B t1(1);
48     t1 = Func(t1);
49 }
50 
51 //Out  put
52 /*
53 Construct  By: 0012FF58  1   //构造函数创建t1
54 Copy  Construct  0012FE5C   0012FF58   //拷贝构造创建临时对象
55 Copy  Construct  0012FE8C   0012FE5C   //拷贝构造函数进行传参(构建对象b)
56 Destructed   0012FE5C  1     //摧毁临时对象
57 operator=   0012FF58   0012FE8C  //赋值函数进行赋值(t1=b)
58 Destructed   0012FE8C  1     //摧毁对象b
59 Destructed   0012FF58  1     //摧毁对象t1
60 */

例4:

 1 /*
 2  *第四种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B Func(B b)
41 {
42     return b;
43 }
44 
45 void main()
46 {
47     B t1(1);
48     B t2 = Func(t1);
49 }
50 
51 //Out  put  
52 /*
53 Construct  By: 0012FF58  1   //构造函数创建对象t1
54 Copy  Construct  0012FE60   0012FF58   //拷贝构造函数创建临时对象
55 Copy  Construct  0012FF4C   0012FE60   //拷贝构造函数传参(b=t1)
56 Destructed   0012FE60  1    //摧毁临时对象
57 Destructed   0012FF4C  1    //摧毁对象b
58 Destructed   0012FF58  1    //摧毁对象t1
59 */

例5:

 1 /*
 2  *第五种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B Func(B &b)
41 {
42     return b;
43 }
44 void main()
45 {
46     B t1(1);
47     B t2 = Func(t1);
48 }
49 
50 //Out  put  
51 /*
52 Construct  By: 0012FF58  1   //构造函数创建对象t1
53 Copy  Construct  0012FF4C   0012FF58   //拷贝构造函数进行赋值(t2=b)
54 Destructed   0012FF4C  1     //摧毁对象t2
55 Destructed   0012FF58  1     //摧毁对象t1
56 */

例6:

 1 /*
 2  *第六种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B & Func(B b)
41 {
42     return b;
43 }
44 void main()
45 {
46     B t1 = 5;
47     B t2 = Func(t1);
48 }
49 
50 //Out  put      
51 /*
52 Construct  By: 0012FF58  5       //隐式类型转换创建对象t1
53 Copy  Construct  0012FE60   0012FF58  //拷贝构造创建临时对象
54 Destructed   0012FE60  5         //摧毁临时对象
55 Copy  Construct  0012FF4C   0012FE60  //拷贝构造函数进行赋值(t2 = b)
56 Destructed   0012FF4C  1244768   //摧毁对象t2  //注意这个特殊启示
57 Destructed   0012FF58  5         //摧毁对象t1
58 */

例7:

 1 /*
 2  *第七种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B & Func(B b)
41 {
42     B t(3);
43     return t;
44 }
45 
46 void main()
47 {
48     B t1(1);
49     B t2(5);
50     t2 = Func(t1);
51 }
52 
53 //Out  put     
54 /*
55 Construct  By  :0012FF58  1   //构造函数创建对象t1
56 Construct  By  :0012FF4C  5   //构造函数创建对象t2
57 Copy  Construct  0012FE60   0012FF58   //拷贝构造函数创建临时对象
58 Construct  By  :0012FE44  3   //构造函数创建对象t
59 Destructed   0012FE44  3      //摧毁对象t
60 Destructed   0012FE60  1      //摧毁临时对象
61 operator=    0012FF4C   0012FE44  //拷贝构造函数进行赋值(t2=t)
62 Destructed   0012FF4C  -858993460 //摧毁对象t2
63 Destructed   0012FF58  1      //摧毁对象t1
64 */

例8:

 1 /*
 2  *第八种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23 //     B &operator=(const B &obj)
24 //     {
25 //         if(this!=&obj)
26 //         {
27 //             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28 //             data=obj.data;
29 //         }
30 //         return *this;
31 //     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B & Func(B b)
41 {
42     B t(3);
43     return t;
44 }
45 
46 void main()
47 {
48     B t1(1);
49     B t2(5);
50     t2 = Func(t1);   //?? 有无赋值函数的区别
51 }
52 
53 //Out  put
54 /*
55 Construct  By  :0012FF58  1   //构造函数创建对象t1
56 Construct  By  :0012FF4C  5   //构造函数创建对象t2
57 Copy  Construct  0012FE60   0012FF58   //拷贝构造函数创建临时对象
58 Construct  By  :0012FE44  3   //构造函数创建对象t
59 Destructed   0012FE44  3      //摧毁对象t
60 Destructed   0012FE60  1      //摧毁临时对象
61 Destructed   0012FF4C  3      //摧毁对象t2
62 Destructed   0012FF58  1      //摧毁对象t1
63 */

例9:

 1 /*
 2  *第九种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B Func(B &b)
41 {
42     B t3(b);
43     return t3;
44 }
45 void main()
46 {
47     B t1=5;
48     B t2=Func(t1);
49 }
50 
51 //Out  put    //分步解析
52 /*
53 Construct  By: 0012FF58  5   //隐式类型转换创建对象t1
54 Copy  Construct  0012FE54   0012FF58  //拷贝构造函数创建临时对象
55 Copy  Construct  0012FF4C   0012FE54  //拷贝构造函数创建局部对象t3
56 Destructed   0012FE54  5   //摧毁临时对象
57 Destructed   0012FF4C  5   //摧毁局部对象t3
58 Destructed   0012FF58  5   //摧毁对象t1
59 */

例10:

 1 /*
 2  *第十种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B Func(B &b)
41 {
42     B t3(b);
43     return t3;
44 }
45 void main()
46 {
47     B t1 = 5;
48     B t2;
49     t2 = Func(t1);
50 }
51 
52 //Out   put   
53 /*
54 Construct  By: 0012FF58  5   //隐式类型转换创建对象t1
55 Default  Construct:  0012FF4C//默认创建对象t2
56 Copy  Construct  0012FE40   0012FF58  //拷贝构造函数创建局部对象t3
57 Copy  Construct  0012FE80   0012FE40  //拷贝构造函数创建临时对象
58 Destructed   0012FE40  5   //函数结束摧毁对象t3
59 operator=   0012FF4C   0012FE80  //赋值函数进行赋值(t2=临时对象)
60 Destructed   0012FE80  5  //摧毁临时对象
61 Destructed   0012FF4C  5  //摧毁对象t2
62 Destructed   0012FF58  5  //摧毁对象t1
63 */

例11:

 1 /*
 2  *第十一种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B & Func(B &b)
41 {
42     B t3(b);
43     return t3;
44 }
45 void main()
46 {
47     B t1 = 5;
48     B t2;
49     t2 = Func(t1);
50 }
51 
52 //Out  put     
53 /*
54 Construct  By: 0012FF58  5  //隐式类型转换构建对象t1
55 Default  Construct:  0012FF4C  //默认构造函数构建对象t2
56 Copy  Construct  0012FE64   0012FF58  //拷贝构造函数创建局部对象t3
57 Destructed   0012FE64  5   //函数结束摧毁局部对象t3
58 operator=   0012FF4C   0012FE64  //赋值函数进行赋值(t2=局部对象)
59 Destructed   0012FF4C  1245004  //摧毁对象t2  //? ?
60 Destructed   0012FF58  5   //摧毁对象t1
61 */

例12:

 1 /*
 2  *第十二种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 B & Func(B &b)
41 {
42     B t3(b);
43     return t3;
44 }
45 void main()
46 {
47     B t1(5);
48     B t2 = Func(t1);
49 }
50 
51 //Out  put   
52 /*
53 Construct  By: 0012FF58  5    //构造函数创建对象t1
54 Copy  Construct  0012FE64   0012FF58  //拷贝构造函数创建局部对象t3
55 Destructed   0012FE64  5  //摧毁局部对象t3
56 Copy  Construct  0012FF4C   0012FE64  //拷贝构造函数进行赋值(t2=t3)
57 Destructed   0012FF4C  1245004  //摧毁对象t2   //? 思考
58 Destructed   0012FF58  5    //摧毁对象t1 
59 */

例13:综合运用类中函数的示范程序:

 1 /*
 2  *第十三种情况:分步解析
 3 */
 4 #include<iostream>
 5 using namespace std;
 6 
 7 class B
 8 {
 9 public:
10     B()
11     {
12         cout<<"Default  Construct"<<"  "<<this<<endl;
13     }
14     B(int i):data(i)
15     {
16         cout<<"Construct  By: "<<this<<"  "<<data<<endl;
17     }
18     B(const B &b)
19     {
20         cout<<"Copy  Construct"<<"  "<<this<<"   "<<&b<<endl;
21         data = b.data;
22     }
23     B &operator=(const B &obj)
24     {
25         if(this!=&obj)
26         {
27             cout<<" operator= "<<this<<"  "<<&obj<<endl;
28             data=obj.data;
29         }
30         return *this;
31     }
32     ~B()
33     {
34         cout<<"Destructed"<<"   "<<this<<"  "<<data<<endl;
35     }
36 private:
37     int data;
38 };
39 
40 void Func(B &b)
41 {
42     B t3(b);
43 }
44 
45 void main()
46 {
47     cout<<"B  t1执行结果:"<<endl;
48     B t1;
49     cout<<"B  t2(5)执行结果:"<<endl;
50     B t2(5);
51     cout<<"B  t3(t2)执行结果:"<<endl;
52     B t3(t2);
53     cout<<"B  t4=6执行结果:"<<endl;
54     B t4=6;
55     cout<<"B  t5=t4执行结果:"<<endl;
56     B t5=t4;
57     cout<<"B  t6执行结果:"<<endl;
58     B t6;
59     cout<<"t6 = t5执行结果:"<<endl;
60     t6=t5;
61     cout<<"Func(t6)执行结果:"<<endl;
62     Func(t6);
63 }
64 
65 //Out  put
66 /*
67 B  t1执行结果:
68 Default  Construct:  0x0012FF70
69 B  t2(5)执行结果:
70 Construct  By: 0x0012FF6C  5
71 B  t3(t2)执行结果:
72 Copy  Construct  0x0012FF68   0x0012FF6C
73 B  t4=6执行结果:
74 Construct  By: 0x0012FF64  6 //饮食类型转换
75 B  t5=t4执行结果:
76 Copy  Construct  0x0012FF60   0x0012FF64
77 B t6执行结果:
78 Default  Construct:  0x0012FF5C
79 t6 = t5执行结果:
80 operator=   0x0012FF5C   0x0012FF60
81 Func(t6)执行结果:
82 Copy  Construct  0x0012FF00   0x0012FF5C
83 Destructed   0x0012FF00  6   //摧毁局部对象t3
84 Destructed   0x0012FF5C  6   //摧毁局部对象t6
85 Destructed   0x0012FF60  6   //摧毁局部对象t5
86 Destructed   0x0012FF64  6   //摧毁局部对象t4
87 Destructed   0x0012FF68  5   //摧毁局部对象t3
88 Destructed   0x0012FF6C  5   //摧毁局部对象t2
89 Destructed   0x0012FF70  0   //摧毁局部对象t1
90 */

 备注:前十二种示例完全可以说明传参与返回时,使用值与引用的区别。后一种主要解释一下类中函数的使用情况,在不同的情况下的调用区别。

posted @ 2013-01-04 20:15  kaizenly  阅读(319)  评论(0编辑  收藏  举报
打赏