初遇构造函数
在翻劉汝佳的紫書(shū)的時(shí)候看見(jiàn)一種奇怪的結(jié)構(gòu)體寫(xiě)法:
struct Edge {int from, to, dist;Edge(int u, int v, int d):from(u), to(v), dist(d) {} };這里有兩個(gè)奇怪的東西
1.?Edge(int u, int v, int d)?結(jié)構(gòu)體里面套了一個(gè)什么玩意?
2.?from(u), to(v), dist(d)?這是什么打法?
問(wèn)了下余翱,第一個(gè)是構(gòu)造函數(shù),可以在main里面調(diào)用從而快速賦值。
例如
... int main() {...vector<Edge> edges;edges.push_back(Edge(from, to, dist));...return 0; }第二個(gè)是元素賦值的另一種方式。
#include<bits/stdc++.h> using namespace std; int a(5),b; int main() {cout << a << "#" << b << endl;return 0; }——輸出是 5#0
?
妙啊妙啊,這樣子就可以賦值結(jié)構(gòu)體了。不然point這些的還要提出來(lái)再操作……
但是這樣子是會(huì)CE的
#include<bits/stdc++.h> using namespace std; struct p{int x; p(int x):x(x) {}; }f[1003]; int main() {return 0; }實(shí)際上是要這樣
#include<bits/stdc++.h> using namespace std; struct p{int x; p() {};p(int x):x(x) {}; }f[1003]; int main() {return 0; }因?yàn)樵趇nt main之前有申請(qǐng)f[],那么前一個(gè)版本并沒(méi)有對(duì)其賦默認(rèn)值,所以沒(méi)法過(guò)編。
至于之后的那一行,是說(shuō)明p這個(gè)結(jié)構(gòu)體初始不進(jìn)行任何操作。
?
找到的一些參考資料:
1.淺談C++中的幾種構(gòu)造函數(shù)
2.構(gòu)造函數(shù)_BaiduBaike
轉(zhuǎn)載于:https://www.cnblogs.com/antiquality/p/8511311.html
總結(jié)
- 上一篇: PHP中调用SVN命令更新网站方法(解决
- 下一篇: [svc]ext4文件删除访问原理