Additional instructions on the referenced c ++ getting started tutorial

Source: Internet
Author: User


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
# Include <string>
Using namespace std;

Void main (int argc, char * argv [])
{
Int a = 10;
Int B = 20;
Int & rn =;
Cout <rn <"|" <a <endl;
Cout <& rn <"|" <& a <endl; // The memory address of the application cannot be obtained in c ++, the referenced address is the target address!
Rn = B; // point the reference to another target ---- variable B
Cout <& rn <"|" <& a <"|" <& B <endl;
Rn = 100; // try to change the value of B
Cout <a <"|" <B <endl; // output the modified result.
Cin. get ();
}



Since the reference itself is an alias of the target, the address of the reference itself is meaningless, so the memory address of the application cannot be obtained in c ++, the referenced address is the target address. c ++ itself does not provide a method to obtain the referenced memory address!

When referencing a single initialization, it will not be able to be directed to other targets. Although the compilation will not go wrong, the operation will not work, but it actually points to the first target!

In the above Code, rn = B is actually a = B on the computer, so the value of a is modified!


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
# Include <string>
Using namespace std;

Void main (int argc, char * argv [])
{
Int a = 10;
Void & rn = a; // The void is of no type.
Int a [100];
Int & ra [100] = a; // error. The referenced array cannot be declared.
Cin. get ();
}


The above two errors should remember the reference feature. The void modifier cannot declare the reference, and the reference cannot declare the array, that is, the reference array cannot be declared!

Next, let's talk about it. It is also the most important and important content to be mastered. It is also a supplement to the memory status of traditional function operations!

Let's take an example.


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
# Include <string>
Using namespace std;

Float c;
Float test (float, float );
Void main (int argc, char * argv [])
{
Float pn = test (3.0f, 1.2f );
Cout <pn;
Cin. get ();
}

Float test (float a, float B)
{
C = a * B;
Return c;
}



In the above Code, we may think that the c variable returned by the function, under normal circumstances, when normal values are returned in the function, a temporary variable is automatically generated in the memory stack space, which is a copy of the returned value and a copy, the function is actually a temporary copy of return during return!

The data in the memory is like!



It clearly indicates the copy of the consular variable!

Let's take a look at another situation, that is, assigning the return value to the reference!


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
# Include <string>
Using namespace std;

Float c;
Float test (float, float );
Void main (int argc, char * argv [])
{
Float & pn = test (3.0f, 1.2f); // warning: a temporary variable is returned, and the pn reference will be the alias of the Temporary Variable!
Cout <pn;
Cin. get ();
}

Float test (float a, float B)
{
C = a * B;
Return c;
}



Float & pn = test (3.0f, 1.2f); this statement can be compiled in bc because the bc extension is set as a reference to the temporary variable setting, the life cycle of the temporary variable will be the same as the referenced life cycle, but it cannot be compiled in vc, because a temporary variable disappears into the stack space after test () is executed, at this time, pn will become a reference with no clear targets. In severe cases, memory errors will occur!

For details about the memory usage, see!



As we can see in the figure, because the function is still normal method return, there will still be a temporary copy variable generated, but this time only returns a target address, in main, the target address is assigned a reference pn

Let's look at another situation. This is the case where the variables are referenced!


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
# Include <string>
Using namespace std;

Float c;
Float & test (float, float );
Void main (int argc, char * argv [])
{
Float pn = test (3.0f, 1.2f );
Cout <pn;
Cin. get ();
}

Float & test (float a, float B)
{
C = a * B;
Return c;
}



In this case, the stack space where test () is located does not generate a temporary variable in the memory, but directly gives the value of global variable c to the variable pn, this method is our most recommended operation method, because the method of directly assigning values without generating temporary variables can save memory space and improve efficiency, and the readability of the program is also good!

For details about the memory usage, see!

The last case is when the function returns a reference and assigns the value to a reference!


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
# Include <string>
Using

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.