Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > + works for string literal also

Reply
Thread Tools

+ works for string literal also

 
 
qazmlp
Guest
Posts: n/a
 
      06-25-2003
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 ;
}
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      06-25-2003

"qazmlp" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: UPDATE: They didn't only pledge allegiance to Mex flag: anchor babies in TX *also* shouted "RAZA! RAZA!" in US school also tedorn44@hotmail.com Digital Photography 9 10-21-2006 11:12 AM
When I turn on my PC, it works, works, works. Problem! Fogar Computer Information 1 01-17-2006 12:57 AM
What's wrong with rpc-literal? Why use doc-literal? Anonieko Ramos ASP .Net Web Services 0 09-27-2004 09:06 AM
After rebooting my PC works, works, works! Antivirus problem? Adriano Computer Information 1 12-15-2003 05:30 AM



Advertisments