[問題] 關於class的member func

看板C_and_CPP (C/C++)作者 (boson18)時間16年前 (2009/04/02 01:48), 編輯推噓4(404)
留言8則, 5人參與, 最新討論串1/2 (看更多)
假設程式碼如下 class abc { public: abc(int xNum);//constructor int membFunc(int here); private: int aNum; } int main() { ...... } abc::abc(int xNum):aNum(xNum) { //constructor initialize } int abc::membFunc(int here) { ...... } 黃色部分是問題所在 因為他並不是class的成員,所以不能用constructor去initialize他 那我程式中有設定他的default value的需求 該怎麼寫會比較好呢 (不知道這樣打大家看不看的懂,囧) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.47.111.165

04/02 01:58, , 1F
int abc::membFunc(int here = 0); 這樣??
04/02 01:58, 1F

04/02 01:58, , 2F
直接寫 int abc::membFunc(int here=初始值即可)
04/02 01:58, 2F
剛才想到的做法 class abc { public: abc(int xNum); membFunc(int here); private: int aNum; int hereInit; } abc abc::memberFunc(int xNum):xNum(aNum),hereInit(here) { //constructor initialize } 不過意義改變了 是用here的值去initialize hereInit,而不再是去initialize here ※ 編輯: boson18 來自: 114.47.111.165 (04/02 02:18)

04/02 02:22, , 3F
你是不是寫錯了
04/02 02:22, 3F

04/02 02:23, , 4F
小弟也覺得, 您是不是把func param default value的東西
04/02 02:23, 4F

04/02 02:24, , 5F
搞得太複雜了一點?? 感覺就是這個func如果caller沒給值
04/02 02:24, 5F

04/02 02:24, , 6F
就給個default value而已, 應該不用class搞成這樣吧@_@"
04/02 02:24, 6F

04/02 02:30, , 7F
搞不懂你想做啥
04/02 02:30, 7F

04/02 11:56, , 8F
你說一說你想達成什麼目的吧? 暫時我只覺得你在亂寫
04/02 11:56, 8F
文章代碼(AID): #19qwZgPA (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #19qwZgPA (C_and_CPP)