C ++ reference

Source: Internet
Author: User

C ++ reference

This article mainly records the reference features in C ++ and summarizes the sample code and errors.

When creating a reference, you must specify the referenced object, and the referenced object will not be modified in the future. A reference is equivalent to a variable alias.

# Include <iostream> # include <string. h> using namespace std; void swapOne (char * & x, char * & y) // pointer reference {char * temp; temp = x; x = y; y = temp;} void swapTwo (char ** x, char ** y) // pointer {char * temp; temp = * x; * x = * y; * y = temp;} void swapThree (char * x, char * y) // pass the pointer value (this method does not work, it is equivalent to passing the value) {// someone may misunderstand this because two integers may have been exchanged using pointers. This is quite different. Char * temp; temp = x; x = y; y = temp;} int main () {char * ap = "Hello"; // * ap content is H, * The content of bp is H char * bp = "How are you? "; Cout <" ap: "<ap <endl; cout <" bp: "<bp <endl; // swapOne (ap, bp ); // swapTwo (& ap, & bp); swapThree (ap, bp); cout <"swap ap, bp" <endl; cout <"ap: "<ap <endl; cout <" bp: "<bp <endl; return 0 ;}# Include <iostream> using namespace std; const float pi = 3.14f; // constant float f; // global variable float f1 (float r) {f = r * pi; return f;} float & f2 (float r) // return reference {f = r * pi; return f; // return the reference of the local variable (note the validity period ), the system runs normally, but the result is incorrect. /* Float ff = f; return ff; */} int main () {float f1 (float = 5); // default parameter 5, you can modify the global variable f = 78.5 float & f2 (float = 5); // same as float a = f1 (); // float & B = f1 (); // In the f1 () function, the value of global variable f is 78.1 assigned to a temporary variable temp (which is implicitly created by the compiler), and then reference B of this temp, an error occurs when referencing a temporary variable temp. float c = f2 (); float & d = f2 (); // The definition variable is not used in the main function, instead, use the reference of global variables directly. This method saves the most memory space among all 4 methods. However, pay attention to the validity period it references. // The validity period of the global variable f must be longer than the reference d, so it is safe. For example, return a reference to a local variable. D + = 1.0f; cout <"a =" <a <endl; // cout <"B =" <B <endl; cout <"c =" <c <endl; cout <"d =" <d <endl; cout <"f =" <f <endl; return 0 ;}# Include <iostream> using namespace std; class Test {public: void f (const int & arg); private: int value ;}; int main () {int a = 7; const int B = 10; // int & c = B; // error: B is a constant, but C is not a constant reference. Correct: const int & c = B; const int & d = a; a ++; // d ++; // error. d is a constant reference. Therefore, Test test; test cannot be modified. f (a); cout <"a =" <a <endl; return 0;} void Test: f (const int & arg) {// arg = 10; // The cout <"arg =" <arg <endl; value = 20 ;}/ For a variable of the constant type, its reference must also be of the constant type. * Variables of a non-constant type can be non-constants. * However, note: under no circumstances can constant references be used to modify the value of referenced variables. */

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.