这个功能有什么问题吗? [英] Anything wrong with this function?
问题描述
遇到了一些总结如下的代码:
char const * MyClass :: errToText(int err)const
{
switch(错误)
{
case 0:return" No error" ;;
case 1:return" not enough ;
案例2:返回太多;
默认:返回未知错误;
}
}
别人说没关系 - 她是对的吗?
Came across some code summarized as follows:
char const* MyClass::errToText(int err) const
{
switch (err)
{
case 0: return "No error";
case 1: return "Not enough";
case 2: return "Too much";
default: return "Unknown error";
}
}
Someone else said it was fine - was she right?
推荐答案
Anonymous写道:
Anonymous wrote:
遇到一些代码总结如下:
char const * MyClass :: errToText(int err)const
{
开关(错误)
{
案例0:返回没有错误;
案例1:返回不够;
案例2:返回太多;
默认值:返回未知错误;
}
}
别人说没关系 - 她说对了吗?
Came across some code summarized as follows:
char const* MyClass::errToText(int err) const
{
switch (err)
{
case 0: return "No error";
case 1: return "Not enough";
case 2: return "Too much";
default: return "Unknown error";
}
}
Someone else said it was fine - was she right?
这取决于你所询问的罚款类型......
正式代码是确实很好。
一个可能的挑剔是,目前''errToText''是'MyClass'类的非静态
成员函数。因为它不需要访问任何
类的成员(至少在这种形式下),也许它应该被转换为_batic_成员函数。也许不吧。很难说这么小的
背景。
-
祝你好运,
Andrey Tarasevich
It depends on what kind of ''fine'' you are asking about...
Formally the code is indeed perfectly fine.
One possible nitpick is that currently ''errToText'' is a non-static
member function of class ''MyClass''. Since it doesn''t need to access any
members of the class (at least in this form), maybe it should be turned
into a _static_ member function. Maybe not. Hard to say with such little
context.
--
Best regards,
Andrey Tarasevich
Anonymous写道:
Anonymous wrote:
>
char const * MyClass :: errToText(int err)const
>
char const* MyClass::errToText(int err) const
Say" const"如果你不打算改变变量
char const * MyClass :: errToText(const int err)const
Say "const" if you are not going to change variable
char const* MyClass::errToText(const int err) const
{
开关(错误)
{
案例0:返回没有错误;
案例1:返回不够;
案例2:返回太多;
默认值:返回未知错误;
}
}
{
switch (err)
{
case 0: return "No error";
case 1: return "Not enough";
case 2: return "Too much";
default: return "Unknown error";
}
}
-
Maksim A Polyanin
--
Maksim A Polyanin
>
Andrey Tarasevich写道:
Andrey Tarasevich wrote:
Anonymous写道:
Anonymous wrote:
>遇到一些代码汇总如下:
char const * MyClass :: errToText(int err)const
{
switch(错误)
{
案例0:返回" ;没有错误" ;;
案例1:返回不够&;;
案例2:返回太多 ;;
默认:返回未知错误;
}
}
其他人说这很好 - 她是对的吗?
>Came across some code summarized as follows:
char const* MyClass::errToText(int err) const
{
switch (err)
{
case 0: return "No error";
case 1: return "Not enough";
case 2: return "Too much";
default: return "Unknown error";
}
}
Someone else said it was fine - was she right?
这取决于你问的罚款类型......
正式的代码是确实很好。
一个可能的挑剔是,目前''errToText''是'MyClass'类的非静态
成员函数。因为它不需要访问任何
类的成员(至少在这种形式下),也许它应该被转换为_batic_成员函数。也许不吧。很难说这么小的
上下文。
It depends on what kind of ''fine'' you are asking about...
Formally the code is indeed perfectly fine.
One possible nitpick is that currently ''errToText'' is a non-static
member function of class ''MyClass''. Since it doesn''t need to access any
members of the class (at least in this form), maybe it should be turned
into a _static_ member function. Maybe not. Hard to say with such little
context.
这里的另一个风格问题是使用幻数。我会做点什么
喜欢:
enum错误
{
NoError,NotEnough ,太多了
};
然后用它而不是int和magic数字来表示错误。
Another style issue here is that magic numbers are used. I''d make something
like:
enum Error
{
NoError, NotEnough, TooMuch
};
and then use that instead of int and magic numbers for the errors.
这篇关于这个功能有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!