4
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!

Community
  • 1
  • 1
linuxeasy
  • 6,269
  • 7
  • 33
  • 40
  • 1
    Well, how is `reply_form::operator+ (...)` defined, what overloads it has, and what it does? Only you can answer this, you have the code... – Remus Rusanu Aug 05 '11 at 18:15
  • 4
    The overload in Edit 2 has nothing to do with the line you are wondering about. `a + b` and `a++` are different operators. – UncleBens Aug 05 '11 at 18:58
  • 1
    "but later I understood it as a syntax and a way of declaring a variable or calling the base class constructor" No. It's a way of initialising data members, or of _explicitly_ invoking the base constructor (as opposed to it being _implicitly_ invoked with no arguments). – Lightness Races in Orbit Aug 05 '11 at 20:41

5 Answers5

9

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;.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
5

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;
}
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
3

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+.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
3

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.

Artyom
  • 31,019
  • 21
  • 127
  • 215
0

For example,

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

would work correct in this case.

Raiv
  • 5,731
  • 1
  • 33
  • 51