Can the string type be added to the string? Can a string's constant quantity be added together?

Source: Internet
Author: User

Procedure 1:

# Include <iostream>
# Include <string>

Int main ()
{
Const STD: String Hello = "hello ";
Const STD: String message = Hello + ", World" + "! ";

STD: cout <message <STD: Endl;

}

Procedure 2:

# Include <iostream>
# Include <string>

Int main ()
{
Const STD: String exclam = "! ";
Const STD: String message = "hello" + ", World" + exclam;

STD: cout <message <STD: Endl;
Return 0;
}

Are both programs correctly compiled? What is the difference?

Program 1 Compilation is normal, and program 2 Compilation is incorrect. The error type is as follows:

Error c2110: cannot add two pointers
In my understanding, a string can be used as a character container and can be added to any string or character. It is equivalent to adding a string or character to the bottom of the character container;

Character string constants cannot be added, so errors may occur. Therefore, when adding a String object to a string, at least one end must be a string object, and two strings or characters cannot be appended.

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.