Reference parameters for C++const types

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 .
    • 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 the left value
See the following example first
voidGstring&a,Const string&b) {cout<<"a address:"<<&a<<Endl; cout<<"Address B:"<<&b<<Endl; //return A;}stringf () {}intMain () {stringinput="I Love You"; intA=1; //int b=2;cout<<"Input Address:"<<&input<<Endl; Const Char*b="***"; cout<<"Address B:"<<&b<<Endl;    g (INPUT,B); //g (input,input);GetChar ();return 0;}

You can see the parameters of the G function one for string& and one for the const string&

we will input= "I Love You" and const char *b= "* * *"; pass in the G function as a parameter to print the address of the pre-and post-incoming variables separately we'll look at the results.

You can see that the const string& parameter that passes the string constant to the G function prints out a different address than the incoming address, which means that the function automatically generates a temporary variable that automatically converts the string constant to the string type.

Then let's look at another example .
1 string f (); 2 string g (string & str); 3 4 g (f ()); 5  G ("ABC");

The program compiles in 4, 5 lines of error, which is why.

The error in line 5th can be explained by the example above, when the reference parameter does not match the argument but can be converted to a reference type to declare the reference to be const.

The error in line 4th knows that when you pass a temporary variable as an argument to a parameter, you also need to set the parameter type to Const.

The above is an experiment on parameters of type string, so are other types of parameters also the same rules?

1 voidGint&a,Const Char&b)2 {3cout<<"a address:"<<&a<<Endl;4cout<<"Address B:"<<&b<<Endl;5     6 //return A;7 }8 9 stringf ()Ten { One } A intMain () - { -  the     intA=1; -     intb=2; -cout<<"a address:"<<&a<<Endl; -cout<<"Address B:"<<&b<<Endl; +      - g (A, b); +      A //g (input,input); at      - GetChar (); - return 0; -}

We changed the parameter parameters to int& and const char& references, passing two shaping variables to the function. The result is the same as above, which also produces temporary variables.

Summary

in C + + When the function argument is a reference.

    • If the argument type of the actual participating function passed does not match, then only the parameter type is defined as const, the function will produce a temporary variable, and the temporary variable is automatically converted to the function parameter type. will be an error.
    • If the passed argument is a temporary variable, then it is necessary to define the function parameter as a const type, or else an error will occur

Reference parameters for C++const types

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.