重载函数的实现
在我的头文件中,我有这个:
std::string StringExtend(const std::string Source, const unsigned int Length, const bool Reverse);
std::string StringExtend(const std::string Source, const unsigned int Length);
在我的 cpp 文件中,我有这个:
std::string Cranberry::StringExtend(const std::string Source, const unsigned int Length, const bool Reverse)
{
unsigned int StartIndex = (Source.length() - 1) * Reverse;
short int Increment = 1 - (Reverse * 2);
int Index = StartIndex;
std::string Result;
while (Result.length() < Length)
{
if (Reverse) Result = Source.at(Index) + Result;
else Result += Source.at(Index);
Index += Increment;
if (!InRange(Index, 0, Source.length() - 1)) Index = StartIndex;
}
return Result;
}
std::string Cranberry::StringExtend(const std::string Source, const unsigned int Length)
{
return StringExtend(Source, Length, false);
}
正如您所看到的,该函数的第二种形式与省略 Reverse
参数完全相同。有没有办法压缩它,或者我是否必须为每种形式提供函数原型和定义?
In my header file, I have this:
std::string StringExtend(const std::string Source, const unsigned int Length, const bool Reverse);
std::string StringExtend(const std::string Source, const unsigned int Length);
In my cpp file, I have this:
std::string Cranberry::StringExtend(const std::string Source, const unsigned int Length, const bool Reverse)
{
unsigned int StartIndex = (Source.length() - 1) * Reverse;
short int Increment = 1 - (Reverse * 2);
int Index = StartIndex;
std::string Result;
while (Result.length() < Length)
{
if (Reverse) Result = Source.at(Index) + Result;
else Result += Source.at(Index);
Index += Increment;
if (!InRange(Index, 0, Source.length() - 1)) Index = StartIndex;
}
return Result;
}
std::string Cranberry::StringExtend(const std::string Source, const unsigned int Length)
{
return StringExtend(Source, Length, false);
}
As you can see, the second form of the function is the exact same thing with the Reverse
argument omitted. Is there a way to condense this, or do I have to have a function prototype and definition for each form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对您的
Reverse
参数使用默认参数。std::string StringExtend(const std::string & Source, unsigned int Length, bool Reverse = false);
摆脱第二个函数:
std::string StringExtend(const std ::字符串和源,无符号整数长度);
Use a default parameter for your
Reverse
parameter.std::string StringExtend(const std::string & Source, unsigned int Length, bool Reverse = false);
Get rid of the second function:
std::string StringExtend(const std::string & Source, unsigned int Length);
您可以为“可选”参数设置默认值,例如
const bool Reverse = false
。You could set a default value for the "optional" parameter, like
const bool Reverse = false
.是,使用 bool 参数的默认参数。例如
std::string StringExtend(const std::string Source, const unsigned int Length, const bool Reverse = false);
那么就不需要第二个函数Yes use default argument for bool parameter. For example
std::string StringExtend(const std::string Source, const unsigned int Length, const bool Reverse = false);
Then there is no need of 2nd function