More on Reference variables

转载 2012年03月23日 21:50:16

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’”.


  • Same address:

            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.


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

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

Strategies for Implementing POSIX Condition Variables on Win32

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

【more effective c++读书笔记】【第5章】技术(5)——Reference counting(引用计数)(2)

三、引用计数基类和智能指针实现的String类 //RCObject.h #ifndef RCOBJECT_H #define RCOBJECT //引用计数基类 class RCObject{ pu...

【more effective c++读书笔记】【第5章】技术(5)——Reference counting(引用计数)(1)

一、非引用计数实现的String类 #ifndef STRING_H #define STRING_H #define _CRT_SECURE_NO_WARNINGS #include class ...

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

本章讲述了很多关于class的关键内容。 1. 首先讲述了分离.h和.m文件 2. 讲述了自动生成setter和getter方法 使用@property和@synthesize 在.h...
  • flks555
  • 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个字)