operator about the string of C + + =

Source: Internet
Author: User

In C + + primer 5, in the section on string, there is this sentence:

string " Hiya ";  // Copy Initialization

In other words, this is the copy initialization, which is called the copy constructor. And the following sentence:

string S6 ("hiya"//  Direct Initialization
But it is also directly initialized, and then I think, in the initialization of C + + above these two are not equivalent? In addition I am also very curious in the implementation of the string string s5 = "Hiya"; If it is true that the copy is initialized, there is no intermediate generation of a temporary variable (though it is unlikely that the write efficiency is obviously very low). So go to see the SGI STL source code, find about operator = part is this:
  operator= (const _chart* __s)  /  WC: = char*    return Assign (__s, _ _s + _traits::length (__s)); }

It is clear that there is no intermediate variable to produce.

I used these two ways to step through vs:

int Main (intChar* argv[]) {       int0;    string " WC " ;    string str2 ("wcww");          return 0

It is then found that both of these initialization statements enter the same constructor, which is the following:

    Basic_string (const _elem *_ptr)        : _mybase ()        {    //  construct from [_ptr, <null>)        _tidy ();        Assign (_ptr);        }

And this is not a copy constructor, just a general constructor. So the structure of the top two sentences belongs to the direct structure without copy construction.

operator about the string of C + + =

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.