References to C + + learning notes

Source: Internet
Author: User

First of all, the content here is mostly "C + + programming ideas" in the content, I recently learned C + +, I feel that a lot of good words inside, as well as examples of their own experiment, some of the phenomenon is very interesting, I hope to share with you.

The reference (Reference) (&) is like a constant-type pointer that can automatically be referenced indirectly by the compiler.

#include <iostream>using namespace STD;intYint& r = y;//When a reference is created, it must be initialized to an object.            //You can also write this:Const int& q = A;//(1) At this point, the reference is bound to a storage location. intx=0;//(2)int& A =x;//(3)intMain () {cout<<"x ="<<x<<", a ="<<a<<endl; a++;cout<<"x ="<<x<<", a ="<<a<<endl; System"Pause");return 0; }

Code Run Result:

At position (1), the compiler allocates a storage unit whose initial value is initialized to 12, and the reference is associated with the storage unit. The point of application is that any reference must be linked to the storage unit. when you access a reference, you access the storage unit. so, adding a is actually adding X, which can be shown in the main () function, and the simplest way to think about a reference is to use it as a fancy pointer, one of the advantages of this pointer is not having to wonder if it was initialized (the compiler forced it to initialize), You don't have to know how to refer to it indirectly (this is done by the compiler).
There are certain rules when using references:

1. When a reference is created, it must be initialized (the pointer can be initialized at any time).
2. Once a reference is initialized to point to an object, it cannot change the reference to another object (the pointer can point to another object at any time).
3. There is no way to have a null reference. You must ensure that the reference is associated with a valid storage unit.

? References in the function:
The most common place to see references is in the parameters and return values of the function. When a reference is used as a function parameter, any changes to the reference within the function will change the parameters outside the function. of course, you can do the same thing by passing a pointer, but the reference has a clearer syntax.
If a reference is returned from a function, it must be treated like a pointer returned from a function, and when the function returns, no matter what the reference is associated with, it will not know which memory to point to.

int& h(){    int q;    return q;//警告,返回的是局部变量或临时变量的地址}

Results appear:

int& h(){    staticint x;    return x;//OK}

This will not be an error.

#include <iostream>using namespace STD;int* F (int* x) {(*x) + +;returnx;}int& G (int& x) {× x + +;returnx;}intMain () {intA=0;cout<<"f (&a) ="<<f (&a) <<endl;cout<<"a ="<<a<<endl;cout<<"g (a) ="<<g (a) <<endl;cout<<"a ="<<a<<endl; System"Pause");return 0; }

The f () function is visible in the address of a, and the address of a is returned.
The G () function passes in the reference to a, which is the value of the storage unit of a that is passed in, and returns the value of the storage unit of a.
The call to function f () lacks the convenience and clarity of using references, but it is clear that this is a pass-through address. In the call to function g (), the address is passed by reference, but it is not visible on the surface.

? Constant reference
This reference parameter will work only if the parameter in the function is a very mass object. If it is a constant object, the function g () will not accept this parameter, which is a good thing, because the function will change the external parameters. If you know that this function does not interfere with the invariance of the object, let this parameter be a constant reference that will allow this function to be used in any case. This means that for user-defined types, the function can only call constant member functions and should not alter any public data members.

#include <iostream>  usingnamespacestd;  void f(int& x){    x++;}void g(constint& x){    x++;}int main()  {      int a=0;    f(1);    g(1);    system("pause");    return0;  }  

Calling F (1) produces a compile-time error because the compiler must first establish a reference that the compiler assigns a storage unit to an int type, initializes it to 1, and bundles it with an address and reference. The stored content must be constant, because it makes no sense to change it, and we can no longer manipulate it. when this data is changed, the compiler will indicate the error, which is a very useful hint, because this change will result in the loss of information.

? Pointer reference

In the C language, if you want to change the pointer itself rather than what it points to, the function declaration might look like this:

void f(int **)

When passing it, you must obtain the address of the pointer:

int47;int* ip = &i;f(&ip);

For references in C + +, the syntax is much clearer. The function parameter becomes a reference to the pointer without having to get the address of the pointer.
int*& I understand the words, compared to int& I. My understanding is int*& I, I first is a reference, followed by a reference to int*, that is, a reference to the pointer.

#include <iostream>  usingnamespacestd;  void increment(int*& i){    i++;}int main()  {      int* i=0;    cout<<"i = "<<i<<endl;    increment(i);    cout<<"i = "<<i<<endl;    system("pause");    return0;  }  

The result is interesting, the definition I is a pointer to the address is 0. After executing the increment (i) function, the pointer adds one because it is a pointer to an integer variable, pointing to the next integer variable and adding 4 to the memory address. So the result is:

There is nothing wrong with the place, but also hope that everyone to correct.

References to C + + learning notes

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.