noob string question

General questions regarding the usage of CodeLite
shorton
CodeLite Curious
Posts: 4
Joined: Sat Aug 09, 2014 2:22 am
Genuine User: Yes
IDE Question: C++
Contact:

noob string question

Post 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?
Old programmer, noob at Codelite and C++, please have mercy and tailor replies accordingly :)
User avatar
eranif
CodeLite Plugin
Posts: 6373
Joined: Wed Feb 06, 2008 9:29 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: noob string question

Post 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
Make sure you have read the HOW TO POST thread
shorton
CodeLite Curious
Posts: 4
Joined: Sat Aug 09, 2014 2:22 am
Genuine User: Yes
IDE Question: C++
Contact:

Re: noob string question

Post 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.
Old programmer, noob at Codelite and C++, please have mercy and tailor replies accordingly :)
Post Reply