From: efoss on
I'm working through some exercises trying to learn C++. I hung up on
this one:

This is OK:

const std::string hello = "Hello";
const std::string = message = hello + ", world" + "!";
std::cout << message << std::endl;

This is not OK:

const std::string exclam = "!";
const std::string = message = "Hello" + ", world" + exclam;
std::cout << message << std::endl;

Why is the second thing not OK?

Thanks.

Eric

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Alexandre Bacquart on
On 07/30/2010 10:18 PM, efoss(a)fhcrc.org wrote:
> I'm working through some exercises trying to learn C++. I hung up on
> this one:
>
> This is OK:
>
> const std::string hello = "Hello";
> const std::string = message = hello + ", world" + "!";

Well, no it is not. Remove the first =. But I get it.

> std::cout<< message<< std::endl;
>
> This is not OK:
>
> const std::string exclam = "!";
> const std::string = message = "Hello" + ", world" + exclam;
> std::cout<< message<< std::endl;
>
> Why is the second thing not OK?

Because there is no implicit conversion from const char* to std::string
in the first argument of the expression. You can resolve it this way:

const std::string message = string("Hello") + ", world" + exclam;

As long as the first in the expression is a std::string, it works, the
rest can be anything that std::string can concatenate with operator+.


--
Alex

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 30 juil, 21:18, "ef...(a)fhcrc.org" <ef...(a)fhcrc.org> wrote:
> I'm working through some exercises trying to learn C++. I hung up on
> this one:
>
> This is OK:
>
> const std::string hello = "Hello";
> const std::string = message = hello + ", world" + "!";
> std::cout << message << std::endl;
>
> This is not OK:
>
> const std::string exclam = "!";
> const std::string = message = "Hello" + ", world" + exclam;
> std::cout << message << std::endl;
>
> Why is the second thing not OK?

I'm assuming you meant const std::string message, not const
std::string = message.

The second is not ok because the + operator is only overloaded in a
meaningful when there are std::string involved, not just string
literals.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jeffrey Schwab on
On 7/30/10 4:18 PM, efoss(a)fhcrc.org wrote:

[Spurious '=' elided]

> const std::string hello = "Hello";
> const std::string message = hello + ", world" + "!";

operator+ has left-to-right associativity, so the first operation
evaluated here is hello + ", world". operator+(std::string, char
const[]) has been declared, so hello + ", world" is fine. The result is
a temporary string. The same function is then called again, with the
temporary as the first arg, and "!" as the second.

> const std::string exclam = "!";
> const std::string message = "Hello" + ", world" + exclam;

No operator+(char const[], char const[]) has been declared, so "Hello" +
", world" is invalid. If operator+ had right-to-left associativity,
this would have been fine, and the first example would have been in error.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Timothy Madden on
efoss(a)fhcrc.org wrote:
> I'm working through some exercises trying to learn C++. I hung up on
> this one:
>
> This is OK:
>
> const std::string hello = "Hello";
> const std::string = message = hello + ", world" + "!";
> std::cout << message << std::endl;
>
> This is not OK:
>
> const std::string exclam = "!";
> const std::string = message = "Hello" + ", world" + exclam;
> std::cout << message << std::endl;
>
> Why is the second thing not OK?

The second thing attempts to evalutate:
("Hello" + ", world") + exclam
which first tries to add a 'char const [6]' array with another 'char
const [8]' array. You can not add arrays with arrays in C++.

The first thing, however, attempts to add a std::string class instance
with 'char const [8]', and class std::string has an user-defined
operator +(), and that allows for the operation.

Timothy Madden

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]