码迷,mamicode.com
首页 > 其他好文 > 详细

临时对象

时间:2016-09-09 20:47:01      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class TPoint
 5 {
 6 public:
 7 TPoint()
 8 {
 9     X=0;
10     Y=0;
11     cout<<"无参数构造函数"<<endl;
12 }
13 
14 TPoint(int x,int y)
15 {
16     X=x;
17     Y=y;
18     cout<<"带参数构造函数"<<endl;
19 }
20 
21 TPoint &operator = (const TPoint &param)
22 {
23     X=param.X;
24     Y=param.Y;
25     cout<<"赋值运算"<<endl;
26     return *this;
27 }
28 
29 TPoint(TPoint &p);
30 
31 ~TPoint() { cout<<"Destructor\n"; }
32 
33 int Xcoord() { return X; }
34 int Ycoord() { return Y; }
35 
36 private:
37     int X,Y;
38 };
39 
40 TPoint::TPoint(TPoint &p)
41 {
42     X=p.X;
43     Y=p.Y;
44     cout<<"copy_constructor\n";
45 }
46 
47 TPoint f(TPoint Q)
48 {
49     cout << endl;
50     cout<<"f函数开始\n";
51     int x,y;
52     x = Q.Xcoord()+10;
53     y = Q.Ycoord()+20;
54     TPoint R(x,y);
55     return R;
56 }
57 
58 void main()
59 {
60     TPoint M(20,35),P(0,0);
61     cout<<"M和P"<<endl;
62     TPoint N(M);
63     cout<<"N"<<endl;
64     P = f(N);
65     cout<<"f函数"<<endl;
66     cout<<"P="<<P.Xcoord()<<","<<P.Ycoord()<<endl;
67     cout<<"main结束?"<<endl;
68 }

技术分享

临时对象

标签:

原文地址:http://www.cnblogs.com/youngqq-16/p/5857826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!