作者boson18 (boson18)
看板C_and_CPP
标题[问题] 关於class的member func
时间Thu Apr 2 01:48:25 2009
假设程式码如下
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
1F:→ james732:int abc::membFunc(int here = 0); 这样?? 04/02 01:58
2F:推 QQ29:直接写 int abc::membFunc(int here=初始值即可) 04/02 01:58
刚才想到的做法
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)
3F:推 QQ29:你是不是写错了 04/02 02:22
4F:推 VictorTom:小弟也觉得, 您是不是把func param default value的东西 04/02 02:23
5F:→ VictorTom:搞得太复杂了一点?? 感觉就是这个func如果caller没给值 04/02 02:24
6F:→ VictorTom:就给个default value而已, 应该不用class搞成这样吧@_@" 04/02 02:24
7F:推 hylkevin:搞不懂你想做啥 04/02 02:30
8F:→ adrianshum:你说一说你想达成什麽目的吧? 暂时我只觉得你在乱写 04/02 11:56