Some Opinions on the Value assignment statement

Source: Internet
Author: User

Symptom:

Today, I wrote a piece of code like this.

vector<string> blankVector(){    return vector<string> blankVector;}

My original intention was to combine the vector <string> blankVector; return blankVector into one sentence. The result is a natural compilation error.

Lujun9972 @ X41 :~ /Study/c/test $ make
G ++-L/usr/lib-o test. cpp
Test. cpp: In the function 'std: vector <std: basic_string <char> blankVector:
Test. cpp: 27: 27: Error: expected primary-expression before 'blance'
Test. cpp: 27: 27: Error: expected '; 'before' blankVector'
Make: *** [test] Error 1


Solution:

In this case, when the C language compiler processes the value assignment statement (the value after the return statement is assigned to the return value, it seems that its right value is expected to be an object rather than a definition..In fact, this code should be modified


vector<string> blankVector(){    return vector<string>();}

Herevector<string>()An anonymous object is generated, and the return Statement is assigned the return value.

Similarly, you can use this method to initialize a class member variable, for example

//A.hclass A{static map<string,string> mss;}//A.cppmap<string,string> A::mss = map<string,string>();

This article is from the "dark day" blog, please be sure to keep this source http://darksun.blog.51cto.com/3874064/1264760

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.