Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   + works for string literal also (http://www.velocityreviews.com/forums/t267682-works-for-string-literal-also.html)

qazmlp 06-25-2003 04:46 AM

+ works for string literal also
 
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 ;
}

Ron Natalie 06-25-2003 02:47 PM

Re: + works for string literal also
 

"qazmlp" <qazmlp1209@rediffmail.com> wrote in message news:db9bbf31.0306242046.3d6d2acb@posting.google.c 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.




All times are GMT. The time now is 07:36 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.