Use of references to C + + references

Source: Internet
Author: User

    • Once a reference is declared, the reference name can only be used as an alias of the target variable name, so the reference name can no longer be aliased to another variable name, and any assignment to that reference is the assignment of the target variable name corresponding to the reference.
    • To address a reference is to address the target variable.
    • Because the pointer variable is also a variable, you can also declare a reference to a pointer variable. The method is:

      Type identifier & Reference name = pointer variable name;

    • You cannot create a reference to an array because the array is a collection of several elements, so you cannot establish a reference to an array.
    • A reference is a reference to a variable or target object, which is not itself a data type, so the reference itself does not account for the storage unit, so that the referenced reference cannot be declared and the referenced pointer cannot be defined.
    • Cannot create a reference to a null pointer, such as: Cannot create int &rp=NULL;
    • It is also not possible to establish a reference to void type void, such as: &ra=3 cannot be established, because although there is a void data type in the C + + language, none of the variables or constants belong to the void type. It is therefore not possible to establish its reference, and the reference is a reference to a target variable, constant, or object, rather than a reference to a type.

I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!

Example: Define a reference to a pointer variable and how to use it.

1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6     int*A;7     int*&p=A;8     intb=Ten;9p=&b; Tencout << *a <<Endl; Onecout << *p <<Endl; A     return 0; - } -  the //Rookie, please criticize advice, code writing habits and norms and so on!!! Thank you!!! 

I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!

Use of references to C + + references

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.