重载函数的实现

发布于 2024-10-07 19:57:34 字数 1057 浏览 0 评论 0原文

在我的头文件中,我有这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲爱恨情仇 2024-10-14 19:57:34

对您的 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);

我早已燃尽 2024-10-14 19:57:34

您可以为“可选”参数设置默认值,例如 const bool Reverse = false

You could set a default value for the "optional" parameter, like const bool Reverse = false.

清风夜微凉 2024-10-14 19:57:34

是,使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文