请解释一下这个 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);
}
在给定的构造函数定义中,请有人解释一下这个语法的含义或其预期用途和好处:
*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 技术交流群。
发布评论
评论(5)
小事:
这段代码
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+
是为了向后兼容并使用
在一些不是最新的例子中。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
由于我们不知道
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 ifreply_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;
.