C + + Reference

Source: Internet
Author: User

In the C + + programming language, a reference is a simple reference datatype that's less powerful but safer than the point ER type inherited from C. The name C + + reference may cause confusion, as in computer science a reference are a general concept datatype with pointer s and C + + references being specific reference datatype implementations. The definition of a reference in C + + is such this it does not need to exist. It can be implemented as a new name for the existing object (similar to rename keyword in Ada).

C + + references differ from pointers in several essential ways:

It is not possible to refer directly to a reference object after it is defined; Any occurrence of its name refers directly to the object it references.

Once A reference is created, it cannot being later made to reference another object; It cannot be reseated. This is often do with pointers.

References cannot be null, whereas pointers can; Every reference refers to some object, although it could or may is not valid. Note that for this reason, containers of references is not allowed.

References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must being initialized as soon as they is created. In particular, local and global variables must is initialized where they is defined, and references which is data member S of class instances must is initialized in the initializer list of the class ' s constructor.

References can thought of as "symbolic links" in the file system terminology. Symbolic links can modified as though they were the file they were linked to and if a symbolic link is deleted the OR Iginal file remains. Similarly, references can be deleted (whether by going out of the scope or explicitly being removed if they were allocated in Heap) and the original object that is referenced remains. Similarly, once a symbolic link have been created it can never be changed.

#include <iostream> #include <stdlib.h>using namespace std;void fun (int &a, int &b); int main (void) { int x = 10;int y = 20;cout << x << "," << y << endl;fun (x, y); cout << x << "," << ; Y << endl;system ("pause"); return 0;} void Fun (int &a, int &b) {int c = 0;c = A;a = B;b = C;}

The program uses references to achieve the interchange of data.

C + + Reference

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.