标签:follow lang sed another idt wap returns 绑定 地址
The reference variable is an alias(So, not allocate memory), Once the reference is initialized to a variable, you can use the reference name or variable name to point to the variable.
#include <iostream> using namespace std; int main(){ int a = 99; // declare simple variables int &b = a; // declare reference variables & 读作引用 }
int &a ; // Error, reference can not be empty must have a reference to the object
#include<iostream> using namespace std; int main(void)
{ int a = 1; int b = 2; int& ra = a; //int& ra = b;// Error, multiple initialization return 0; }
可以在修改形参的同时对实参的修改。
不存在对象复制,避免了对象的开销。
从函数中返回引用,一定要保证在函数返回以后,被引用的目标一直有效,也就是不能返回函数体内的局部对象的引用,
局部对象离开作用域就会被析构掉,所以不能返回对函数体内的局部对象的引用。
#include <iostream> using namespace std; void swap1(int a, int b); void swap2(int *p1, int *p2); void swap3(int &a, int &b); int main(){ int num1, num2; cout<<"Input two integers: "; cin>>num1>>num2; swap1(num1, num2); cout<<num1<<" "<<num2<<endl; cout<<"Input two integers: "; cin>>num1>>num2; swap2(&num1, &num2); //指针则是地址调用 cout<<num1<<" "<<num2<<endl; cout<<"Input two integers: "; cin>>num1>>num2; swap3(num1, num2); cout<<num1<<" "<<num2<<endl; return 0; } //直接传递参数内容 void swap1(int a, int b){ int temp = a; a = b; b = temp; } //传递指针 void swap2(int *p1, int *p2){ int temp = *p1; *p1 = *p2; *p2 = temp; } //按引用传参 void swap3(int &a, int &b){ //函数 形参 & int temp = a; a = b; b = temp; }
#include <stdio.h> int main() { int a = 12; const int &ra = a; //const reference a =42; // ra =42 ; //Error: The value of the target variable can not be modified by a const reference printf("a= %d,ra= %d\n",a,ra); return 0; }
int Task1(int x, double y); // uses return by value int& Task2(int x, double y); // uses return by reference
Similar to pass by address(类似于传递地址), values returned by reference must be variables (you can not return a reference to a literal or an expression(文字或表达式的引用)). When a variable is returned by reference(通过引用返回变量), a reference to the variable is passed back to (传递给)the caller. The caller can then use this reference to continue modifying the variable.
However, just like return by address, you should not return local variables(局部变量) by reference. Consider the following example:
int& doubleValue(int x) { int value = x * 2; return value; // return a reference to value here } // value is destroyed here
#include <iostream> using namespace std;
// Global variable int num;
// Function declaration int& test(); int main() { test() = 5; cout << num; return 0; } int& test() { return num; }
In program above, the return type of function test() is int&. Hence, this function returns a reference of the variable num.
The return statement is return num;. Unlike return by value, this statement doesn‘t return value of num, instead it returns the variable itself (address).
Important Things to Remember When Returning by Reference
int& test() { return 2; }
int& test() { int n = 2; return n; }
#include <stdio.h> int globalvar = 20; int& foo() { return globalvar; } int main() { foo() = 10; //left value printf("globalvar is : %d\n",globalvar); return 0; }
#include <iostream> using namespace std; int & squareRef(int &); int * squarePtr(int *); int main() { int number1 = 8; cout << "In main() &number1: " << &number1 << endl; // 0x22ff14 int & result = squareRef(number1); cout << "In main() &result: " << &result << endl; // 0x22ff14 cout << result << endl; // 64 cout << number1 << endl; // 64 result = 100 ; // adress ,result alias cout << result << endl; // 100 cout << number1 << endl; // 100 int number2 = 9; cout << "In main() &number2: " << &number2 << endl; // 0x22ff10 int * pResult = squarePtr(&number2); cout << "In main() pResult: " << pResult << endl; // 0x22ff10 cout << *pResult << endl; // 81 cout << number2 << endl; // 81 } int & squareRef(int & rNumber) { cout << "In squareRef(): " << &rNumber << endl; // 0x22ff14 rNumber *= rNumber; return rNumber; } int * squarePtr(int * pNumber) { cout << "In squarePtr(): " << pNumber << endl; // 0x22ff10 *pNumber *= *pNumber; return pNumber; }
Reference is another means of(手段) generating a polymorphic effect in addition to the pointer. This means that a reference to a base class can point to its derived(派生) class instance.
标签:follow lang sed another idt wap returns 绑定 地址
原文地址:http://www.cnblogs.com/yunfung/p/7613279.html