Use a C-style string as a String object reference parameter

Source: Internet
Author: User

    • The string class defines a char*-to-string conversion function, which allows a C-style string to be used to initialize a string object.
    • One of the parameters of a parameter of type const reference indicates that: Assuming that the argument type of the argument does not match the reference parameter, but can be converted to a reference type, the program creates a temporary variable of the correct type, initializes it with the converted argument value, and passes a reference to the temporary variable.

      From the above two properties we know: If the formal parameter type is const string &, when the function is called, the argument used can be a string object or a C-style string, such as a string literal enclosed in quotation marks, a char array ending with a null character, or a pointer variable pointing to char.
    • The above refers to a property where the const reference is a formal parameter, that is, if the referenced parameter is const, the compiler generates temporary variables in some cases, such as the following two cases:
      1, the argument type is incorrect, but can be converted to the correct type.
      2, the argument type is correct, but not an lvalue.

  • Let's look at the code example.
    1#include <iostream>2#include <string>3  4  using namespacestd;5  6  stringVersionstring&a,Const string&b);7  8  intMainvoid)9  {Ten      stringinput ="I Love you.";  Onecout <<"Input Address:"<< &input <<Endl; A      Const Char* B ="***"; -cout <<"I b Address:"<< &b <<Endl; -   the version (input, b); -   -      return 0; -  } +   -  stringVersionstring&a,Const string&b) +  { A      using namespacestd; atcout <<"a address:"<< &a <<Endl; -cout <<"Address B:"<< &b <<Endl; -      returnA//without this, a memory error is generated. Returns a reference.  -  } -  /************************************ - * Input address:0x7ffe09a647f0 in * I b address:0x7ffe09a647e8 - * a address:0x7ffe09a647f0 to * b address:0x7ffe09a64840 +   * **********************************/

    where input is a string object, the string class is applied to define a char*-to-string conversion function that converts the string literal to a string object. B is the char* type, and as an argument the function creates a temporary variable. Looking at the code, the address of B in main is:I b address:0x7ffe09a647e8 , and the address in version is:b address:0x7ffe09a64840 . They are different, stating that the function is not a reference to passing a char *b, but a temporary variable. Also see the string class input, in main and in version address is the same. The description is a reference that passed input.

Use a C-style string as a String object reference parameter

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.