Re: [問題] 想要把pair的first跟second「換個名字」

看板C_and_CPP (C/C++)作者時間16年前 (2010/05/09 13:50), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《CrBoy (上大學好忙喔)》之銘言: : 這是我依照想法寫的 : struct MyType: public pair<int,int>{ : MyType():height(first),weight(second){} : MyType(int h, int w):height(first),weight(second){height=h, weight=w;} : MyType(pair<int,int> p):height(first),weight(second) : {height=p.first,weight=p.second;} : int &height, &weight; : }; 這樣改如何: template <class T, class S> struct MyType: public pair<T, S> { #define MYTYPE_INIT height(first), weight(second), pair<T, S> MyType() : MYTYPE_INIT() {} MyType(T h, S w) : MYTYPE_INIT(h, w) {} MyType(pair<T, S> p) : MYTYPE_INIT(p) {} #undef MYTYPE_INIT T &height; S &weight; }; 只是把不清爽的部份塞到macro裡 但是這個macro還是要寫3次 XD -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.34.186.254
文章代碼(AID): #1Bvar16A (C_and_CPP)
文章代碼(AID): #1Bvar16A (C_and_CPP)