Reference to a reference

V

Victor Bazarov

Neo said:
OK.. heres what i understood about references.

void swap(int &a, int &b)
{
int t=a,a=b,b=t;

This declares three objects, 't', 'a', and 'b', all local, all with
automatic storage duration. It does not swap 'a' and 'b' arguments.
It attempts to _hide_ them. Unsuccessfully, of course. Curiously,
if it worked, 'a' would only be hidden after 't' is assigned its value
from the "real" 'a'. And 'b' would only be hidden after the local 'a'
gets its value from the "real" 'b'...

You should probably think of using semicolons instead of commas there:

int t=a; a=b; b=t;

otherwise, it's not well-formed.
}
int main()
{
int c=5,d=10;
swap(c,d);
/* here i thought that when compiling, c++ treats a nd b as c and d.

Yes, and...
So i assumed i cannot use the function swap more than one time as a
and b cannot be substituted by another e and f.

Every time the function is called, its formal arguments are initialised
from the actual arguments. That's the only way to "assign" a reference.
if we indeed can then the compiler is internally looking into the
matter and substituting the pointers as required(we can see this in
the disassembly). this makes the reference arguement as a macro (which
saddens me)

What am i missing?

Probably nothing. There are two kinds of references: those that have the
same scope as the referred objects, and those that don't. The ones that
don't have to be implemented somehow. If the function is inlined, we
might assume that the references in the inner scopes truly become aliases
but not all functions can be inlined. So, an alternative implementation
of references is needed. A pointer is it. Why it would sadden anybody
is beyond my comprehension.

V
 
M

makc.the.great

part about newsgroups not being the place for
private conversations
it's up to me to decide. really.
as long as _you_ understand your
own posts, you don't care that everyone else can't follow them.
1st, you != everyone
2nd, if you can't follow, you can't say anything good, so why saying
anything at all.

I just don't get it. If you care, you will "follow", if you don't - you
just don't, there's no reason to attach multikilobytes of quotes.
 
K

Kai-Uwe Bux

it's up to me to decide. really.

Actually, the nature of this medium is for nobody to decide. It so happens
to be public by design.


[snip]
I just don't get it.

No need to state the obvious.

If you care, you will "follow", if you don't - you
just don't, there's no reason to attach multikilobytes of quotes.

a) You might want to read a little bit about propagation of posts in usenet.
It is actually not at all guaranteed that the history of this thread is
present on, e.g., my server. Thus quoting is not just a matter of curtesy,
it is a reasonable measure to deal with a technological feature of this
medium.

b) Read up on proper trimming. Nobody here is talking "multikilobytes."


Best

Kai-Uwe Bux
 
N

Neo

Thanks Vic.. but i still dont understand what you mean by 'HIDE'. If
you could tell me how the compiler puts in the binary i could
undersatnd.

Also am i right in assuming that the simple function i wrote cannot be
used twice in the main fn. according to C++ standard.

Also are reference arg functions inine by default.

Neo
 
T

TIT

Neo sade:
Thanks Vic.. but i still dont understand what you mean by 'HIDE'. If
you could tell me how the compiler puts in the binary i could
undersatnd.

The compiler has nothing to do with it. Perhaps a book would suffice.

void swap(int &a, int &b)
{
int t=a,a=b,b=t;
}

This function (above) that you wrote is ill-formed, as you try
to declare 'a' and 'b' twice. A corrected version:

void swap(int &a, int &b)
{
int t=a;
a=b;
b=t;
}
Also am i right in assuming that the simple function i wrote cannot be
used twice in the main fn. according to C++ standard.

Of course this corrected function can be used several times in main
and elsewhere:

int main() {
int a = 0, b = 1, c = 3, d = 4, e = 5;
swap(a,b);
swap(c,d);
swap(e,b);
swap(d,c);
swap(a,e);
return 0;
}

Consider this rewriten version of swap with pointers:

void swap( int * a, int * b ) {
int t = *a;
*a = *b;
*b = t;
}

int main() {
int a = 0, b = 1, c = 3, d = 4, e = 5;
swap(&a,&b);
swap(&c,&a);
swap(&e,&b);
swap(&d,&c);
swap(&a,&c);
return 0;
}
Also are reference arg functions inine by default.

No. Just because an argument is a reference doesn't mean
that the function is suitable to be inlined.

TIT
 
T

Thomas J. Gritzan

Neo said:
Thanks Vic.. but i still dont understand what you mean by 'HIDE'. If
you could tell me how the compiler puts in the binary i could
undersatnd.

#include <iostream>

int i = 5;

int main()
{
int i = 42;

std::cout << i << std::endl;
}

What is the output of the program?

It is 42, because the local variable i in function main *hides* the
other variable i on file scope. I.e. if two variables with the same name
exist, the name finds the variable of the inner-most block.

Thomas
 
V

Victor Bazarov

Neo said:
Thanks Vic.. but i still dont understand what you mean by 'HIDE'.

If you define a scope inside another scope (a compound statement inside
another compound statement, for example), any object declared in the
inner scope hides the object with the same name in the outer scope. It
doesn't work with function arguments, obviously, since they are declared
in the scope of the function body. But if you defined another compound
statement inside, you could hide the arguments:

void foo(int i)
{ // scope of 'i' begins here
{ // define another, nested, scope
int i = 42; // just another 'i', not the foo's argument
i = 33; // the inner 'i' is changed here
} // end of inner scope
i = 30; // outer 'i' is now changed
}
If
you could tell me how the compiler puts in the binary i could
undersatnd.

I am not sure what you mean here.
Also am i right in assuming that the simple function i wrote cannot be
used twice in the main fn. according to C++ standard.

No, that's not true. It can be used as many times as you wish.
Also are reference arg functions inine by default.

No.

V
 
N

Neo

If
you could tell me how the compiler puts in the binary i could
undersatnd.


I am not sure what you mean here.
-------------------------------------
I mean what are the variables passed to the fn. onto the stack.

Ok lets consider
void swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}

in the main:
swap(c,d);//1
swap(e,f); //2 this wont give an error

What are the variables passed on to the stack. Are they the addresses
of te vars c,d,e,f??

If yes, then the exact same is done in the case of Void swap(int *a,
int *b).... swap(&c,&d);... so how is the passing thru reference any
different from passing thru pointeres..

If no.... well could someone explain what happens at the machine level
(as in what is exaxtly passed on the stack.. or if the stack isn't
involved at all)

...Or should i post the whole thing at alt.compilers???

Neo
 
J

John Harrison

Neo said:
I am not sure what you mean here.
-------------------------------------
I mean what are the variables passed to the fn. onto the stack.

Ok lets consider
void swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}

in the main:
swap(c,d);//1
swap(e,f); //2 this wont give an error

What are the variables passed on to the stack. Are they the addresses
of te vars c,d,e,f??
Yes.


If yes, then the exact same is done in the case of Void swap(int *a,
int *b).... swap(&c,&d);... so how is the passing thru reference any
different from passing thru pointeres..

The syntax is more convenient. Imagine writing an assignment operator,
or copy constructor with no references

X::X(const X* rhs)
X* X::eek:perator=(const X* rhs)

X a;
X b(a); // doesn't compile
X c = a; // doesn't compile
b = a; // doesn't compile

Also you cannot pass NULL to a swap with references
If no.... well could someone explain what happens at the machine level
(as in what is exaxtly passed on the stack.. or if the stack isn't
involved at all)

At the implementation level references are exactly like pointers. It is
at the C++ language level that there are differences. For instance a
pointer can be assigned to point at something else, but a reference cannot.

john
 
J

Jim Langston

Senthil said:
Hi,
I am reading Modern C++ Design where Anderi quoted "C++ does not allow
references to references".

Assume i have
DoThat(const string& strData2)
{
...
}

DoThis(const string& strData)
{
..
DoThat(strData);
}

int main()
{
string Data;
DoThis(Data);
}

Now the strData i have inside DoThis is a reference to Data.The
strData2 I have is a reference to strData.i.e strData2 is a reference
to a reference.
Here Andrei's statement confuses me..Am i missing something?

Thanks,
Senthil

char** MyPointer; could be considered a pointer to a pointer.
char&& MyReference; could be considered a reference to a reference, which is
illegal.
 
O

Oliver S.

Also, references are necessary for operator overloading:

Not only operator-overloading but copy-constructors as well!
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top