Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

Reference

How a reference to an object is implemented? With the reference a locally declared object can be accessed from another called function. How this is implemented without
passing the address of the object?
0
leena_mary
Asked:
leena_mary
1 Solution
 
Answers2000Commented:
If you mean - how does it work internally : The machine code generated by the compiler uses the address of the object.  The important part is the C++ programmer isn't aware that there is address manipulation going on.

On most compilers these 2 functions will generate identical machine code

void SomeFunc1( int& x ) // reference
{
x = 1 ; // modify reference
}

void SomeFunc2( int * x ) // pointer
{
*x = 1 ;
}


You call these :-

int a ;
SomeFunc1(x) ; // reference call

int b ;
SomeFunc2(&b) ; // pointer call
0
 
pagladasuCommented:
Answers2000 is right. When a reference to an object is passed as an argument to a function, in reality the address of the object is passed.

0
 
leena_maryAuthor Commented:
Here are your points Answers2000.
Thank u pagladasu for your comment.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now