Acclerated C ++ chapter 1st

Source: Internet
Author: User

1-1: Correct

1-2: Incorrect

STD: String exclam = "! ";
STD: String message = "hello" + ", World" + exclam; error! (Error c2110: cannot add twopointers)

STD: String message = exclam + "hello" + ", World"; correct!

STD: String message = "hello" + exclam + ", World"; correct!

 

Question 1 and Question 2:

String-type phase +, not an addition operation, is essentially a string connection.

Originally in C ++, string constants (like "hello") are not STD: string type, but character arrays, or C-style strings.

(1) const STD: String message = "hello" + ", World"
+ Exclam;

Const STD: String message = (const char [6] + const char [8]) + STD: string );

("+" Is left-bound)

First addition: converted to const char [6] + constchar [8].

However, this addition is not defined for the Array (I .e., the addition operator is not overloaded for the array ).

Although arrays are implicitly converted to pointers, const char * + const char * does not define addition operations between two pointers.

The compiler does not know that you want to convert the result to the STD: string type.

 

(2) const STD: String message = exclam + "hello" + ", World"

Const STD: String message = (STD: String
+ Const char [8]) + const char [2]

The first addition is converted to STD: string + const char *. Such an addition operation is defined and the return type is STD: string. AndThe result is a string..

The second addition is converted to STD: string + const char.

========================================================== ====================================

 

1-3: Correct

1-4: Correct} changed to};}, no effect

1-5: Incorrect vc6 error: Error c2065: 'X': undeclaredidentifier

 

Explanations 3, 4, and 5:

(1) variables defined in {} are valid only in {}, and are not valid outside.

(2) The program code segment uses {} to represent a sentence. Add a ";" after a sentence, which is still a sentence.

========================================================== ====================================

 

1-6:

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.