# More on Reference variables

More on references

• Reference variables are aliases (an alternate name to a variable).

int x;
int &ref=x;        //we tell the compiler that ‘ref’ is another name for ‘x’

Working on ‘ref’ is the same as working on ‘x’ and vice-versa.

But since a reference variable is an alias, we should initialize the alias.

int x;
int &ref;            //ILLEGAL reference variable should be initialized.

This makes sense because an alias should be an alternate name for something (and we need to tell the compiler what that something is).

• Non-const references should be initialized using L-values (i.e. using something which is permitted on the left hand side of an assignment expression).

int &ref=5;       //ERROR

This is an error because ‘5’ is an integer constant and it is not an L-value (we cannot use 5 on the left side of an assignment). Logically speaking also this isn’t possible because a reference variable is an alias (you use an alias instead of another name). One might argue, “instead of the constant 5, I intended to use the term ref”. But when we think from the viewpoint of the compiler, we can understand the complications involved if this were permitted. Let’s say this statement was legal. Now (though the programmer might not purposely do it), there is nothing to stop the programmer from coding:

int &ref=5;       //ERROR (but let’s assume this is permitted)
ref=10;

What now; where is the new value of 10 supposed to be stored? (there is no location for it).

But if we use a const-reference:

const int &ref=5;          //Permitted

the compiler won’t complain. The reason is that now we tell the compiler, “This is a const reference and in case I attempt to assign any values to it then you can flag an error”. The compiler feels reassured and it will permit the use of ‘ref’ instead of 5.

Note: For the above purpose, it would be more clearer and effective to use

const int ref=5;

(since we are not really making use of any functionality of a reference variable).

• We have seen that passing by reference is similar to passing a pointer (as was done in C programming). In fact reference variables and pointers are quite similar. You might wonder, “why not use pointers itself?”

When passing a pointer, the function definition will be cluttered with a lot of dereferencing.

 Passing pointers (C style) Passing by reference void square (int *x) { *x = (*x) * (*x); } void square (int &x ) { x = x * x; }

The code is clean and reader-friendly when passing by reference.

• We can use the ‘const’ keyword in reference parameters to ensure that the function doesn’t modify the argument. For example:

//Function won’t modify the argument

void print(const int &x)
{
cout<<endl<<"The value is:"<<x;
}

void square (int &x)
{
x = x * x;
}

int main ( )
{
int y=5;
print(y);
square(y);
print(y);
return 0;

The advantage of using void print(const int &x) is that when someone reads the code later they will immediately know that the function print( ) doesn’t modify the argument.

The following:

void square (const int &x)         //compiler error
{
x = x * x;
}

is an error. The compiler will complain saying, “you are attempting to modify the value of the constant reference ‘x’”.

int x;
int &ref=x;
cout<<endl<<&x;         //address of ‘x’
cout<<endl<<&ref;      //address of ‘ref’

Both addresses will be the same (which is why working on ‘x’ is the same as working on ‘ref’).

• We can return references from functions but beware.

int& dummy( )
{
int x=5;
return x;
}

int main ( )
{
cout<<dummy( );
return 0;

The function dummy( ) is supposed to return a reference. Though the code might work as expected (an output of 5); the compiler will issue a warning (sometimes you might also get unexpected results). The warning is: “reference to local variable x' returned”. The variable ‘x’ would be destroyed when we reach the end of the function dummy( ). A reference is just an alias. Thus when ‘x’ is destroyed, we shouldn’t use the alias of ‘x’ as well. The rule is: Don’t return references in such scenarios.

• 本文已收录于以下专栏：

## Effect of Different Variables on Overall Vertical Jump Height

• 2017年10月12日 09:06
• 76KB
• 下载

## Comment on James J. Heckman, Instrumental Variables A study of Implicit Behavio

• 2013年02月07日 15:58
• 331KB
• 下载

## C语言 WEEK2 - FOR循环 & Function & Scanf & more Variables

/* print "*" 55 times */ #include void main(){ int i ; for(i = 0 ; i< 55 ; i = i+1 ) printf("*"...

## CSharp - Module 8_Using Reference-Type Variables

• 2008年08月24日 11:39
• 675KB
• 下载

## Strategies for Implementing POSIX Condition Variables on Win32

From:http://www.cs.wustl.edu/~schmidt/win32-cv-1.html Strategies for Implementing POSIX Conditi...

## Objective-C程序设计第七章----More on Classes

• flks555
• 2015年06月25日 00:17
• 269

## 出现 No more output is seen on the console after "booting the kernel" 调试信息

Another problem a user may encounter when booting the kernel, is that no further output after "booti...

## JS(Coursera):More On Arrays

Array You’ll learn some advanced array functions SORTINGarray.sort() Alphabetical order var pets = [...

举报原因： 您举报文章：More on Reference variables 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)