请解释一下这个 C++语法似乎不完整或缺少赋值?

发布于 11-27 22:29 字数 1295 浏览 0 评论 0原文

reply_form::reply_form()
{
    using cppcms::locale::translate;
    author.message(translate("Author"));
    comment.message(translate("Comment"));
    send.value(translate("Send"));
    *this + author + comment + send;
    author.limits(1,64);
    comment.limits(1,256);
}

在给定的构造函数定义中,请有人解释一下这个语法的含义或其预期用途和好处:

*this + author + comment + send;

对我来说,似乎是在没有将其分配给任何东西的情况下完成的串联/加法,这真的让我感到困惑,因为我没有能够理解其目的。

我已经在网络上对这个问题进行了很好的搜索,但可能我没有使用正确的术语来搜索它,因为我不知道这种类型的语法到底是什么。

我觉得它好像是最新 C++1x 功能的一部分,但不确定,所以无法找到合适的书。最后选择 stackoverflow 进行救援:-)

编辑:

我可以提供的最相关的是它在头文件中的声明,即:

struct reply_form : public cppcms::form {
    cppcms::widgets::text author;
    cppcms::widgets::textarea comment;
    cppcms::widgets::submit send;
    reply_form();
};

EDIT2:Revision2 (在检查注释后再次更正)

好的,我能够追溯操作员+的事情,并发现重载的方式:

    inline form &operator + (form &f)
    {
        add(f);
        return *this;
    }

这就是运算符重载的情况。感谢您的回答。我最近迁移到 C++,并发现了一些奇怪的事情,正如我在此线程中发布的: 解释 C++ 中的空白类函数,在我看来,它是带有空白主体的多功能列表,但后来我把它理解为一种语法,一种声明变量或调用基类构造函数的方式。

感谢您的回答,它非常有帮助而且非常快!

reply_form::reply_form()
{
    using cppcms::locale::translate;
    author.message(translate("Author"));
    comment.message(translate("Comment"));
    send.value(translate("Send"));
    *this + author + comment + send;
    author.limits(1,64);
    comment.limits(1,256);
}

In the given constructor definition, someone please explain what this syntax means or its intended use and benefits:

*this + author + comment + send;

To me, it appears as if a concatenation/addition which is done without it being assigned to anything, and thats really confusing me as I am not able to understand its purpose.

I have made good searches on web with the problem, but probably, I am not using the right term to search for it as I do not know what exactly this type of syntax is termed as.

I feel as if its the part of latest C++1x features, but not sure, so not able to find the right book for it. Finally chose stackoverflow for rescue :-)

EDIT:

the Most relevant I could provide is its declaration in header file, which is:

struct reply_form : public cppcms::form {
    cppcms::widgets::text author;
    cppcms::widgets::textarea comment;
    cppcms::widgets::submit send;
    reply_form();
};

EDIT2:Revision2 (corrected again after checking comments)

Ok, I was able to traceback the operator+ thing, and found out the way it was overloaded:

    inline form &operator + (form &f)
    {
        add(f);
        return *this;
    }

So this was the case of operator overloading. Thanks for your answer. I have been recently migrated to C++ and had found some strange things as I had posted in this thread:
Explain blank class functions in C++ which appeared to me as multifunction list with blank body, but later I understood it as a syntax and a way of declaring a variable or calling the base class constructor.

Thanks for your answers, it were really helpful and very quick!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

南城旧梦2024-12-04 22:29:09

由于我们不知道reply_form是什么,所以这个问题无法完整回答。你的问题更多的是关于程序而不是关于C++。

如果 reply_form::operator+ 有副作用,则 *this +author + comment + send;可能有副作用。

但按照惯例,它不会(因为正如您所发现的那样,它没有直观意义)。因此,这要么是类设计中的错误,要么该行应为 *this +=author + comment + send; 之类的内容。

Since we don't know what reply_form is, this question cannot be answered in completion. Your question is more about the program than about C++.

The line *this + author + comment + send; can have side-effects if reply_form::operator+ has side-effects.

Conventionally, though, it would not (because it doesn't make intuitive sense, as you've discovered). So, either this is a fault in the class's design, or the line should read something like *this += author + comment + send;.

半山落雨半山空2024-12-04 22:29:09

最有可能的是重写流畅的运算符以将字段添加到 reply_form。例如。

reply_form& reply_form::operator+ (const field& value)
{
    this->add_field (value);
    return *this;
}

Most likely is a fluent operator overwritten to add fields to a reply_form. Eg.

reply_form& reply_form::operator+ (const field& value)
{
    this->add_field (value);
    return *this;
}
本宫微胖2024-12-04 22:29:09

由于在 C++ 中,几乎所有运算符都可以重载,因此它可能意味着任何内容。实际上,有问题的行是对函数 operator+ 的嵌套调用序列:

operator+(operator+(operator+(*this, author), comment), send);

您需要查看 reply_form 类如何重载 operator+ >。

Since in C++, almost all operators can be overloaded, it could mean anything. In reality, the line in question is a sequence of nested calls to the function operator+:

operator+(operator+(operator+(*this, author), comment), send);

You need to look at how the reply_form class has overloaded operator+.

半葬歌2024-12-04 22:29:09

小事:

这段代码

reply_form::reply_form()
{
    using cppcms::locale::translate;
    author.message(translate("Author"));
    comment.message(translate("Comment"));
    send.value(translate("Send"));
    *this + author + comment + send;
    author.limits(1,64);
    comment.limits(1,256);
}

可以而且应该写成:

reply_form::reply_form()
{
    using cppcms::locale::translate;

    author.message(translate("Author"));
    author.limits(1,64);

    comment.message(translate("Comment"));
    comment.limits(1,256);

    send.value(translate("Send"));

    add(author);
    add(comment);
    add(send);

}

实际上新代码应该使用这个更详细但清晰的 API。

保留 operator+ 是为了向后兼容并使用
在一些不是最新的例子中。

Small thing:

This code

reply_form::reply_form()
{
    using cppcms::locale::translate;
    author.message(translate("Author"));
    comment.message(translate("Comment"));
    send.value(translate("Send"));
    *this + author + comment + send;
    author.limits(1,64);
    comment.limits(1,256);
}

Can and should be written as:

reply_form::reply_form()
{
    using cppcms::locale::translate;

    author.message(translate("Author"));
    author.limits(1,64);

    comment.message(translate("Comment"));
    comment.limits(1,256);

    send.value(translate("Send"));

    add(author);
    add(comment);
    add(send);

}

And actually new code should use this more verbose but clear API.

The operator+ is kept for backward compatibility and is used
in some not-up-to-date examples.

瀞厅☆埖开2024-12-04 22:29:09

例如,

reply_form& operator+(reply_form& lValue,const reply_form& rValue);

在这种情况下可以正确工作。

For example,

reply_form& operator+(reply_form& lValue,const reply_form& rValue);

would work correct in this case.

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