Essential C + + learning notes

Source: Internet
Author: User

1. When we invoke a function, we create a special area in memory called the "program Stack", which provides the storage space for each function parameter, and it also provides the memory space of each object defined by the function-we refer to these objects as local objects. Once the function is complete, the memory is freed, or the pop comes out of the program stack.

2.Pass by reference semantics

Reference plays the role of an indirect number card between the outside and the object, as long as the & symbol is inserted between the type name and the reference name, a reference is declared:

1 int ival=1024x768;   // object, type int 2 int *pi=&ival;    // pointer (pointer), pointing to an int object 3 int &rval=ival;   // Reference (Avatar), representing an int object

When we write this:

1 int jval=4096; 2

, the Jval is assigned to the object represented by Rval (that is, ival). We cannot rval instead of representing Jval, because C + + does not allow us to change the objects represented by reference, they must be mindedness when we write:

1 pi=&rval;

, we are actually assigning the address of ival (which is the object represented by Rval) to Pi. We did not point Pi to Rval. Note that the point is that all operations that face reference are like the indistinguishable of operations performed by the "Reference object". This is also true when we use reference as a function parameter.

When the swap () function assigns val2 to VAL1:

1 void swap () 2 {3// The value of the actual parameter will thus change 4int temp=val1; 5 val1=val2; 6 val2=temp; 7 }

, it is true that VEC[JX] is assigned to vec[ix]--, the former is the Val2, the latter is the object represented by Val1:

1 Swap (VEC[IX],VEC[JX]);

When we pass an object as a function argument by reference, the object itself does not replicate another copy--the address of the object is copied. Any action on the object in a function is tantamount to an indirect operation on an incoming object.

One of the reasons to declare a parameter as reference is that you want to be able to modify the incoming object directly. This reason is extremely important because, as we saw in the previous example, the program does not work correctly if you do not do so.

The second reason to declare a parameter as reference is to reduce the burden of copying large objects. This reason is less important than it is because it is only an efficiency issue.

3. For example, now I'm going to show the vector passed in to display (), which means that every time I want to display, all the elements inside the vector will be copied. This does not cause errors, but it can be faster if you pass in the vector address directly. This can be achieved by declaring the vector's parameters as reference.

 1  void  display ( const  vector<int  > &vec)  2  { 3  for  (int  Ix=0 ; ix<vec.size (); ++ix)  4  { 5  cout<< Vec[ix]<< " "   6  cout<<ENDL;  7  }  8  }

We have declared a reference to const vector, because the function does not change the contents of the vector, the lack of a const does not cause errors, but with the const can let the reader know, we pass the way to the vector, To avoid duplication, not to modify it in a function.

If we want to, we can also pass the vector in the form of pointer. this is the same as the utility passed as reference: the address of the object is passed, not the copy of the entire object. The only difference is that reference and pointer are used differently , for example:

1 voidDisplayConstVector <int> *VEC)2 {3       if(!VEC)4       {5cout<<"display (): The vector pointer is 0\n";6         return ;7 }8 9  for(intix=0; Ix<vec->size (); + +ix)Ten { Onecout<< (*vec) (ix) <<" "; Acout<<Endl; -}
1 intMain ()2 {3    intia[8]={1,2,1, A,2,2,1,1};4vector<int> Vec (ia,ia+8);5 6cout<<"Vector before sort";7Display (&AMP;VEC);//Incoming Address8    //The rest of the code9}

The more important difference between the pointer parameter and the reference parameter is that the pointer may (or may not) point to an actual object, and when we pick up pointer, Be sure to first determine that the value is not 0. As for reference, it is bound to represent an object, so this check is not required.

In general, unless you want to change the value of a parameter within a function, do not use a pass-through method when passing in a built-in type, which is primarily used as a pass-through class object.

Essential C + + learning notes

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.