reference types in C + +

Source: Internet
Author: User

A reference type is also called an alias, and it is a very interesting thing. In C + + you can think of it as another pointer, by reference type We can also indirectly manipulate the object, the reference type is mainly used in the formal parameters of the function, usually we use it is to pass the class object to a function.

Reference objects are defined by the type name plus the & symbol and name. For example: (int &test;), here we define a reference named test for type int, but int &test; is not able to be compiled successfully because the definition of a reference must be assigned to the application at the same time. The assignment here does not mean that the value of the variable is passed to the reference, but rather the reference is directed to the variable, written in this way: (int &test= variable name;).

#include <iostream>
using namespace Std;
void Main (void)
{
int a=10;
int &test=a;
test=test+2;
cout << &a << "|" << &test << "|" << a << "|" <<test << endl;
Cin.get ();
}

Observe and compile the code above you will find that the address of &a and &test is the same, and the value of a and test is the same!

In conjunction with the contents of the previous tutorial, let's take a look at the relevant content of the const reference, and pay special attention to the idea that a reference with a const modifier like the previous tutorial is also confusing!

The const modifier, if used on a reference, has a special point, and its secret is that it can be initialized with different types of objects, which are not possible on ordinary variable operations. Let's take a look at an example:

#include <iostream>
using namespace Std;
void Main (void)
{
int a=10;
Double &test = a + 1.2f; This sentence is wrong!
Const double &test = a + 1.2f;
cout << &a << "|" << &test << "|" << a << "|" <<test << endl;
Cin.get ();
}

The above code is enough to explain the problem, this is the benefit of the Const modifier, but the smart person will find a problem in the output, that is, the output of the value of a and test is different, according to the first point should be able to change the value of a, why is there any change here?

The reason is that, after the const modifier, the reference is changed inside the compiler.

int a=10;
Const double &test = a + 1.2f;

Such a piece of code in the compiler think is the following way

int a=10;
int temp = A;
Const DOUBLE &test = temp + 12.f

This is actually assigning a value of a to a temporary temp variable, and then test gets the temp+12.f change is temp instead of a, so there's a situation where the values shown in a and test are different, pay special attention to this is a very confusing place, When writing a program to be particularly careful, so as not to have a problem but can not check why!

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.