Max*_*xpm 0 c++ overloading function
在我的头文件中,我有这个:
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);
Run Code Online (Sandbox Code Playgroud)
在我的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);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,函数的第二种形式与Reverse省略的参数完全相同.有没有办法压缩这个,还是我必须为每个表单都有一个函数原型和定义?
使用参数的默认Reverse参数.
std::string StringExtend(const std::string & Source, unsigned int Length, bool Reverse = false);
摆脱第二个功能:
std::string StringExtend(const std::string & Source, unsigned int Length);