Learn C ++ in a practical way & (reference)

Source: Internet
Author: User

1. Example 1:

Hypothesis:

Int var1 = 224; // The memory address that stores the value of var1 224 is 0x12abcdef

The following code:

Int var2 = var1; // copy the value of var1 to var2.int & var2 = var1; // var2 is the alias of var1 (var2 is a reference of var1), var1 = var2 = 224, the value of var1 has not been copied. The address of the values is 0x12abcdefint var2 = & var1; // error. & var1 is the address where the value of var1 is 224. (& Here it means getting the address)

Var2 is the alias of var1, meaning that each variable name represents a block data in the memory, while var2 and var1 represent the same block, so the following:

Var2 = 1104; // var1 equals 1104

2. Example 2:

Class Foo {public: Int & setvar1 () {return var1;} // return an alias for a member variable VAR of the foo class void setvar2 (Int & T) {var2 = T ;} // when calling a function to pass parameters, pass private: int var1; int var2 ;}; Foo; int I = 224; Int J = 1104; Foo. setvar () = I; // reference can be used as the left value. The Foo member var1 is 224foo. setvar2 (j); // The member var2 of foo is now 1104.

When the setvar2 method is called, because the type of the parameter T is reference (Int &), there is no copy action when passing the real parameter. Because the type of var2 is defined as int (instead of Int &), var2 = T is worth copying. The value of J is copied to var2. Modifying the value of J does not affect var2.

3. 3rd examples

Class bigsizeclass
{
// This is a class that occupies a lot of memory space
....
};
Void func1 (const bigsizeclass & var); // call the function to PASS Parameters in reference mode. The parameter is passed only as the alias of the variable, rather than copying the VaR of the bigsizeclass instantiated object, pass it to func1 ().
Void fun2 (const bigsizeclass * var); // The parameter only points to the address of the variable. During the real parameter transfer process, VaR is not copied.
// From this point of view, & is similar to *. When a function is called for parameter passing, no real parameters are copied, reducing the time space overhead during the call.

4. 4th examples

Class Foo {public: Foo () {m_var = 1104;} Int & VAR () {return m_var;} PRIVATE: int m_var;}; Foo. vaR () = 224; // (1) Obtain Foo. m_var is an alias, so Foo. m_var changes to 224.int I = Foo after being assigned a value. vaR (); // (2) the type of I is int, not Int &, so I opened up a new memory space, and the program will Foo. the m_var value 224 is copied to the memory space. Int & J = Foo. var (); // (3) the type of J is Int &. It is the alias of foo. m_var and points to the memory space of foo. m_var. Return 0;

5. 5th examples

Class Foo {public: Foo () {m_var = 0;} int fun1 () {return m_var;} Int & fun2 () {return m_var;} PRIVATE: int m_var ;} int main () {Foo; int var1 = Foo. fun1 (); // correct, Int & var2 = Foo. fun1 (); // error, compiler error // invalid initialization of non-const reference of Type 'int & 'from a temporary of Type 'int' int var3 = Foo. fun2 (); // correct, review the 1st example Int & var4 = Foo. fun2 (); // correct, var4 is foo. m_var reference var4 = 4; // correct, now Foo. m_var = 4 ,}

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.