-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.delegate_constructor.cpp
More file actions
168 lines (132 loc) · 3.13 KB
/
9.delegate_constructor.cpp
File metadata and controls
168 lines (132 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// Created by satellite on 2023-04-14.
//
//委托构造函数
/*
* 委派构造函数也是C++11中对C++的构造函
* 数的一项改进,其目的也是为了减少程序员书写构造函数的时间。通
* 过委派其他构造函数,多构造函数的类编写将更加容易
* */
/*class Info{
public:
Info():type(1),name('a'){InitRest();}
Info(int i):type(i),name('a'){InitRest();}
Info(char e):type(1),name(e){InitRest();}
private:
void InitRest(){*//*其他初始化*//*}
int type;
char name;
//...
};*/
/*这里的3个构造函数都声明了初始化列表
来初始化成员type和name,并且都调用了相同的函数InitRest。可以看
到,除了初始化列表有的不同,而其他的部分,3个构造函数基本上是
相似的,因此其代码存在着很多重复。*/
//可以使用c++11提供的成语初始化来简化:
/*class Info{
public:
Info(){InitRest();}
Info(int i):type(i),name('a'){InitRest();}
Info(char e):type(1),name(e){InitRest();}
private:
void InitRest(){*//*其他初始化*//*}
int type = 1;
char name = 'a';
//...
};*/
//这样确实使构造函数变得简单了,但是每个构造函数都需要调用InitRest函数
/*
class Info{
public:
Info(){InitRest();} //基准版本的构造函数
Info(int i):Info(){type=i;} //在初始化列表中委托构造函数完成一部分初始化工作
Info(char e):Info(){name = 'a';}
private:
void InitRest(){*/
/*其他初始化*//*
}
int type{1};
char name{'a'};
//...
};
*/
/*
* 委派构造函数只能在函数体中**为type、 name等成员赋初值,这是由于委派构造函数不能有初始化列表
* 造成的。在C++中,构造函数不能同时“委派”和使用初始化列表**,
* 所以如 果委派构造函数要给变量赋初值,初始化代码必须放在函数体中*/
/*class Info{
public:
Info(){InitRest();}
Info(int i):Info(){type=i;}
Info(char e):Info(),name('a'){} //无法即使用委托构造又使用初始化列表
private:
void InitRest(){*//*其他初始化*//*}
int type{1};
char name{'a'};
//...
};*/
#include "iostream"
using namespace std;
class Info {
public:
Info() {
InitRest();
cout << "info() type = " << type << endl;
} //基准版本的构造函数
Info(int i) : Info() {
cout << "Inof(int) type =" << type << endl;
type = i;
cout << "Inof(int) type =" << type << endl;
} //在初始化列表中委托构造函数完成一部分初始化工作
Info(char e) : Info() { name = 'a'; }
void InitRest() {
type++;
}
int type{1};
char name{'a'};
//...
};
/*class Info{
public:
Info():Info(1,'a'){}
Info(int i):Info(i,'a'){cout<<"目标构造"<<endl;}
Info(char e):Info(1,e){}
private:
Info(int i,char e):type(i),name(e)
{
cout<<"委托构造"<<endl;
type++;
}
int type;
char name;
//...
};
int main()
{
Info f(3);
//cout<<f.type<<endl;
}*/
#include <list>
#include <vector>
#include <deque>
using namespace std;
class TDConstructed{
template<class T> TDConstructed(T first,T last):l(first,last){}
list<short> l;
public:
TDConstructed(vector<short> &v) : TDConstructed(v.begin(),v.end()){}
TDConstructed(deque<int> &d) : TDConstructed(d.begin(),d.end()){}
void show()
{
for(auto & i : l)
{
cout<<i<<" ";
}
}
};
int main()
{
vector<short>vi = {12,13,35};
TDConstructed t(vi);
t.show();
}