Page 1 of 1

noob string question

Posted: Sun Aug 10, 2014 1:40 am
by shorton
Learning c++ using Codelite.

This works:
string z
z="";
z=z+"123"+"4";

but this will not compile: (invalid operands...binary operator)
z= "123" + "4" ;

I found from trial and error I had to start any concatenation assignment with a string variable, otherwise it won't compile.

Can someone help me understand why?

Re: noob string question

Posted: Sun Aug 10, 2014 4:05 am
by eranif
This is because std::string has an operator + overloaded, but not char*
So writing:

Code: Select all

std::string + const char*
will return a reference to std::string ( and this why you can add another const char* )

While:

Code: Select all

const char* + const char*
is undefined

But these kind of questions should really be posted on stackoverflow.com or any c++ forum, but not here

Eran

Re: noob string question

Posted: Sun Aug 10, 2014 4:43 am
by shorton
But these kind of questions should really be posted on stackoverflow.com or any c++ forum, but not here
Thank you, Appreciate the pointer to c++ help.