+ works for string literal also

Q

qazmlp

int main()
{
std::string strVal = "2" ;
strVal = "000" + strVal ;
}

I am wondering how the above one compiles well. I expected that, it should be

int main()
{
std::string strVal = "2" ;
strVal = std::string("000") + strVal ;
}
 
R

Ron Natalie

qazmlp said:
int main()
{
std::string strVal = "2" ;
strVal = "000" + strVal ;
}

I am wondering how the above one compiles well. I expected that, it should be
An overload for operator+ is provided
string operator+(const char*, const string& )

(Not literally, but the above is good enough to understand the issue).

This means you can mix most operations you can do with strings with char*'s pointing
at null terminated strings as long as one operand is a string type (obviously no overload
is possible if both args are pointers).

"000" + str;
str + "000"
str + str;

are all valid

"000" + "000"

is not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

get() attribute for Entry in Tkinter 1
Help with UNIONS 8
Printing out the objects stack 0
Newbie - getting value from excel cel 1
CIN Input #2 gets skipped, I don't understand why. 1
TF-IDF 1
Crossword 2
Crossword 14

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top