By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,847 Members | 2,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,847 IT Pros & Developers. It's quick & easy.

References in C++

P: n/a
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 .

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
apoorv wrote:
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.

Jul 23 '05 #2

P: n/a
apoorv wrote:
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++.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3

P: n/a
Ioannis Vranos wrote:
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++.



--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4

P: n/a
Ioannis Vranos wrote:
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.


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.

Jul 23 '05 #5

P: n/a

Rolf Magnus wrote:
Ioannis Vranos wrote:
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.


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

Jul 23 '05 #6

P: n/a
"apoorv" <ar**********@nospam.quark.com> wrote in message
news:e8******************************@localhost.ta lkaboutprogramming.com...
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
Jul 23 '05 #7

P: n/a

apoorv wrote:
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

Jul 23 '05 #8

P: n/a
apoorv wrote:
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.
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.