clsas成员函数的默认参数 [英] default params to clsas member functions

查看:81
本文介绍了clsas成员函数的默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



不确定标准:哪一个是正确的,如果哪一个更好?
更好?


class FOO {

void foo(const string&,const string& username =" true");

.....

}


void FOO :: foo(const string& cycle,const string& username =" true")

{

........

};





class FOO {

void foo(const string&,const string&);

.....


};


void FOO :: foo(const string& cycle,const string& username =" true")

{

........

}


thx


not sure of the stardard: which one is right, if both which one is
preferable?

class FOO{
void foo(const string &, const string &username="true");
.....

}

void FOO::foo(const string & cycle, const string &username="true")
{
........
};

OR

class FOO{
void foo(const string &, const string &);
.....

};

void FOO::foo(const string & cycle, const string &username="true")
{
........
}

thx

推荐答案

puzzlecracker写道:
puzzlecracker wrote:
不确定标准:哪一个是正确的,如果哪一个更好?
FOO类{
void foo(const string&,con st string& username =" true");
....

}

void FOO :: foo(const string& cycle,const string& username =" true")


禁止重新定义默认参数值。

{

.......

};

类FOO {
void foo(const string& ,, const string&);
....

};

void FOO :: foo(const string& cycle,const string& username = true)
$

.......

}
not sure of the stardard: which one is right, if both which one is
preferable?

class FOO{
void foo(const string &, const string &username="true");
....

}

void FOO::foo(const string & cycle, const string &username="true")
Redefinition of a default argument value is prohibited.
{
.......
};

OR

class FOO{
void foo(const string &, const string &);
....

};

void FOO::foo(const string & cycle, const string &username="true")
{
.......
}




最好在声明中使用默认参数值,即在您的情况下,在类定义中使用
。原因是类的用户使用默认的

参数值。如果传递给它的值

,那个特定成员函数的实现通常不会太在意因为它在代码中被省略了,或者因为它是

指定与默认值完全相同的值。并且

类的用户通常看不到实现。那么,他可以获得默认的

值?


V



Preferable to have the default argument value in the declaration, i.e.
in your case, in the class definition. The reason is that the default
argument value is used by the users of the class. The implementation
of that particular member function usually can''t care less if the value
passed to it was because it was omitted in the code or because it was
specified to the exact same value as the default. And the user of the
class normally can''t see the implementation. So, how is the default
value going to be available to him?

V


puzzlecracker写道:
puzzlecracker wrote:
不确定标准:哪一个是正确的,如果哪一个更好?
not sure of the stardard: which one is right, if both which one is
preferable?




你必须把默认的论点放在呼叫者看到的声明中。

。这几乎肯定意味着它在类定义中定义了




你不能在另一个声明或定义中再次指定它。 br />

你的例子中的Niehter是对的。这是正确的方式


class FOO {

void foo(const string&,const string& username =" true"); //这里

};


//不在这里

void FOO :: foo(const string& cycle,const string& ;用户名){



You have to put the defaulted argument at the point of declaration that
the caller sees. This almost certainly means that it''s defined
in the class definition.

You can''t specify it again in another declaration or definition.

Niehter of your examples are right. This is the right way

class FOO {
void foo(const string&, const string& username="true"); // HERE
};

// not here
void FOO::foo(const string& cycle, const string& username) {


>您必须将默认参数放在调用者看到的
>You have to put the defaulted argument at the point of declaration that
的声明点。这几乎肯定意味着它在类定义中被定义了。
你不能在另一个声明或定义中再次指定它。
the caller sees. This almost certainly means that it''s defined
in the class definition.
You can''t specify it again in another declaration or definition.



you不能或不应该吗?


you CANNOT or SHOULDNOT?


这篇关于clsas成员函数的默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆