复制构造函数

 1 /* 复制构造函数 */
 2 
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 // 编译器会为任何一个类生成默认的构造和拷贝构造
 8 // default保留 delete删除
 9 class myclass
10 {
11 public:
12     myclass(const myclass & my) = delete;// 删除某个函数
13     myclass() = default;// 保留某个函数
14 
15 }
16 int main()
17 {
18 
19     myclass my1;
20     //myclass my2(my1);
21 
22     std::cin.get();
23     return 0;
24 }

拷贝构造实战:

 1 /* 拷贝构造实战 */
 2 
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 
 8 class myclass
 9 {
10 public:
11     int x;
12     int y;
13     myclass():a(10),y(8)// 自定义构造
14     {
15     
16     }
17     
18     // 自定义拷贝构造
19     myclass(const myclass & my)
20     {
21         this->x = my.x;
22         this->y = my.y;
23     }
24 
25     void print()
26     {
27         cout << this->x << " " << this->y << endl;
28     }
29 
30 }
31 
32 int main()
33 {
34     myclass my1;
35     my1.print();
36     my1.x = 123;
37     my1.print();
38     myclass my2(my1);
39     my2.print();
40 
41     std::cin.get();
42     return 0;
43 }

 

posted on 2015-06-06 07:32  Dragon-wuxl  阅读(111)  评论(0)    收藏  举报

导航