About Adding strings to other types in C ++ and C #

Source: Internet
Author: User

Everyone is familiar with the string addition operation. Generally, there are several situations:

1. When the two strings are directly added, C # creates a new string by creating an intermediate temporary object. Its value is the connection between the two strings.

For example, in C #, string STR = "A" + "example"; in C ++, strings are added.

2. When a string is added to an integer or boolean type. In C #, we know that each type inherits from the object, and all objects implement the tostring () method by default. In this way, when I execute a string and an integer, or other non-string addition methods, the compiler automatically calls the tostring () method of the object, converts it to a string, and then executes the addition of two strings.

However, in C ++, When you directly add strings and integers, it is not the above result.

Example: cout <"test" + 3 <Endl;

What do you think is the result? Haha, It will surprise you. In fact, this statement executes pointer operations, which is equivalent to headpointer + 3. In this case, the result is "T ". When the number exceeds the length of the string, the string is empty. In fact, this is very simple, but we do not pay attention to it at ordinary times.

I hope this will help you in the future.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.