-
January 26th, 2004, 09:38 AM
#1
Ampersand usage in C++ ?
Guys I got a question,
When you use the Ambersand sign "&" infront of a variable it means we are passing the address of that variable , right?
what happents when you have the "&" at the end of the variable .
Ex :
if I have a function header
exam(const K&, const V&);
what is the usgae of & in the above?
and what is the difference between this one
exam(const &K, const &V);
-
January 26th, 2004, 10:17 AM
#2
When you use the Ambersand sign "&" infront of a variable it means we are passing the address of that variable , right?
Correct,
Code:
int x=10;
int* y=&x;
In this example we are passing the address of x to the pointer y.
what happents when you have the "&" at the end of the variable .
This refers to passing by reference.
Code:
int i=1;
int& r=i; // r and i now refer to the same int
int x=r; // x=1
r=2; // i=2
*Note, references must be initialized. Also important, the reference type and variable type should NOT differ, otherwise c++ will create a hidden object that does not alias the specified value.
Code:
int value;
float& alias=value;
Very bad, the types differ.
Code:
int value;
int& alias;
Also bad, alias is not initalized.
There are reasons for using references, one is that they use a cleaner syntax than passing pointers.
When you pass a reference to a function, you eliminate copying. So if you have a very large object and don't want to modify it in the function, passing it as a
is a good idea.
Last edited by bluesource; January 26th, 2004 at 11:15 AM.
-
January 26th, 2004, 10:37 AM
#3
The first statement
exam(const K&, const V&);
is a valid funtion declartion if K and V are types. This function's parameters are refenence.
However the second statement
exam(const &K, const &V);
I do not understand. const is a key word and if K and V are types then why & is positioned before a type£¿ It is illegal against c++ grammer.
-
January 26th, 2004, 10:39 AM
#4
exam(const &K, const &V);
I do not understand. const is a key word and if K and V are types then why & is positioned before a type£¿ It is illegal against c++ grammer.
That is a good point, I was confused by this part of the question.
The OP was confused, in the 1st posting Luda says:
what happents when you have the "&" at the end of the variable .
when Luda probably meant: "what happens when you have the "&" at the end of the type."
Likewise, Luda probably meant
Code:
exam(const K &value, const V &value2);
which is the same thing as:
Code:
exam(const K& value, const V& value2);
So to answer the final question, what is the usage of exam(const K&, const V&); ?
Take the following example:
Code:
exam(const K&, const v&);
K a=10;
K& b=a;
V c=11;
V& d=c;
exam(b,d);
This illustrates passing by reference, your function exam will modify the a and c variables by reference, even though you passed b and d in.
Last edited by bluesource; January 26th, 2004 at 11:10 AM.
-
January 26th, 2004, 11:38 AM
#5
I hope this wasn't overly confusing...but now after I've thought about it I think your confusion can be summarized as follows:
Code:
int x=10;
int& y=x;
We say y references x.
But this is saying the same thing as
Code:
int x=10;
int &y=x;
Once again we say y references x even though they were declared differently. The & does not refer to the address.
So
Code:
int x=10;
int* y=&x;
Here the & refers to the address, whereas in the above two examples it does NOT.
Last edited by bluesource; January 26th, 2004 at 12:05 PM.
-
January 26th, 2004, 02:23 PM
#6
Thanks!
Thanks guys!
I guess my question was a lil confusing. sorry.
but Im all clear now.
-
January 26th, 2004, 02:25 PM
#7
Re: Thanks!
Originally posted by LudaLuda
I guess my question was a lil confusing. sorry.
Don't worry, the answers have been not less confusing...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|