References in C++

A

apoorv

can anybody explain me how exactly C++ lang implements references.I read it
from some where that it is nothing but const pointer internally but few of
my seniors told me its not that than what is it how its implemented....plz
help me
thanks in advance .
 
I

Ioannis Vranos

apoorv said:
can anybody explain me how exactly C++ lang implements references.


This is upon the implementation to decide.

I read it
from some where that it is nothing but const pointer internally but few of
my seniors told me its not that than what is it how its implemented....plz


It may be implemented in such a way, however in many cases a reference
may be optimised out of the compiler, so as a separate "reference
object" does not exist.

This is why references have no size nor a memory address of their own in
ISO C++.
 
R

Rolf Magnus

apoorv said:
can anybody explain me how exactly C++ lang implements references.I read
it from some where that it is nothing but const pointer internally but few
of my seniors told me its not that than what is it how its
implemented....plz help me

Typically, references are indeed internally implemented the same way as
pointers. I've never heard of any other way of implementing them, but that
doesn't mean much.
 
I

Ioannis Vranos

Ioannis said:
It may be implemented in such a way, however in many cases a reference
may be optimised out
by

the compiler, so as a separate "reference object" does not exist.

This is why references have no size nor a memory address of their own
in ISO C++.
 
R

Rolf Magnus

Ioannis said:
It may be implemented in such a way, however in many cases a reference
may be optimised out of the compiler, so as a separate "reference
object" does not exist.

However, there is no reason why in such situations a pointer couldn't be
optimized away, too.
This is why references have no size nor a memory address of their own in
ISO C++.

However, they still might take up detectable storage, e.g. if used as a
class member.
 
M

msalters

Rolf said:
However, there is no reason why in such situations a pointer couldn't be
optimized away, too.

Actually, there is: pointers, unlike references, are PODs and can
be memcpy'd. That restricts the ability to optimize them away - it's
much harder to detect all the reads of and writes to a pointer.

HTH,
Michiel Salters
 
M

Mike Wahler

apoorv said:
can anybody explain me how exactly C++ lang implements references.

No, nobody can, because the language does not define how
they're implemented, only how they behave. Their specific
implementation is up to the compiler.
I read it
from some where that it is nothing but const pointer internally

Many compilers implement references as 'hidden' pointers,
but there's no requirement that any do.
but few of
my seniors told me its not that

They should have told you that it might be that, but might not.
than what is it how its implemented

It depends upon the compiler. Why do you need to know?

-Mike
 
D

Default User

apoorv said:
can anybody explain me how exactly C++ lang implements references.

You're making a fundamental mistake. The language doesn't implement
anything, it only provides a specification for behavior.
Implementations (duh) implement things.




Brian
 
E

E. Robert Tisdale

apoorv said:
Can anybody explain me how exactly C++ language implements references.

The C++ computer programming language does *not* implement anything.
Compiler developers "implement" the language.
I read it from some where that
it is nothing but const pointer internally

A reference is just another name (a synonym) for an object.

int i = 13; // an object of type int named i
int* p = &i; // a pointer to an object of type int
int& k = *p; // another name for i

The pointer p is an object but the reference k is not.
k is just another name for i (or *p in this case).
Pass by reference:

void f(int& k);

and pass by reference through a pointer:

void g(int* p);

are implemented exactly the same way -
the compiler generates code to pass the address of k (or *p)
[by value] to the function.
but few of my seniors told me [that]
it's not that than what is it how it's implemented.

I can't parse that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,651
Latest member
VeraPiw932

Latest Threads

Top