Possible Duplicate:
Difference between pointer variable and reference variable in C++
As I am starting with C++, I found the operation below confusing. I got to know about passing by reference and passing by value. But recently I came across functions like this which confused me:
Func1(int &a)
Func2(int *a)
Both of the functions expect the address of a, but when I call Func1 I do that by Func1(a)
and in case of Func2, I call by Func2(&a)
.
How come Func1 is accepting int a directly while it is e开发者_C百科xpecting the address of a?
Func1(int &a)
// It accepts arguments by reference.
// Changes to an inside Func1 are reflected in the caller.
// 'a' cannot bind to an Rvalue, e.g., it can't call Func1(5)
// 'a' can never be referring to something that is not a valid object
Func2(int *a)
// It accepts arguments by value.
// Change to an inside Func1 is not reflected in the caller, and changes to *a are
// 'a' can bind to an Rvalue, e.g., Func1(&localvar).
// 'a' can be NULL. Hence Func2 may need to check if 'a' is NULL.
When providing an argument for a pass-by-reference parameter, the compiler will do the necessary & operation behind the scenes. You, as the programmer, know that it is internally using the address of your argument, but this is hidden in the pass-by-reference abstraction. This is safer than using a pointer as you cannot inadvertently reassign a reference.
Internally, there's not much difference. But one is a reference, and the other one is a pointer. The primary difference is that you can't modify the reference in your function, so the reference will always point to a
.
I.e.,
void func1(int &a) {
a = 5;
}
This will modify a
, i.e., whichever variable the caller pointed to.
void func2(int *a) {
*a = 5; // Same effect as the above code
a = &b; // You couldn't do that with a reference
}
Basically, when you have a &something you have a reference and this is nothing more than a pointer which cannot change. In other words, it is a const pointer, so basically it’s like *something, but in this case you can change the pointer (to point somewhere else) :)
A quick example:
Reference: Object &obj
The same written with pointer syntax: Object* const obj
Func1 will take a reference to an int, and Func2 will take a pointer to an int. When you do Func2(&someint), you're giving the function the address of someint. This address can be dereferenced to get its value. When you "pass by value" Func1(int someint), then, a copy of someint is performed. When you pass either by reference, or by pointer, no such copy is performed.
A reference can be thought of, as essentially an "alias" to the original value, or, another way of referring to it. Yes, it's abstract, but, everything else is implementation-specific and not for you to worry about.
In Func1(int &a)
, &a
is the reference to the variable that you will be passing.
In Func2(int *a)
, *a
is the pointer to address of the variable that you will be passing by its address.
When the function definition is Func1(int &a), it means this function will accept the address of the variable which will pass to this function as a parameter. (So you don't need to take care of passing the address of the variable which will a parameter to the function). The function default behavior will be to get the address of the passed variable.
E.g.,
Func1(int &a){
a = 5; // 'a' will store 5 at &a
}
Func1(a) // We don't need to pass &a. The function definition will take care of this.
===================================================================
Whereas if the function definition is Func2(int *a), it means it will hold the address of the given value. That means you must have to pass &a in the function call as a parameter, which will be later stored as in *a in the function definition.
E.g.,
Fun2(int *a){
*a = 7; // a will store 7 at &a
}
Function call: Fun2(&a); Must have to pass &a. The function definition will not take care of this.
It is not like that.
Func1(int &a)
- when you call this function,Func1(a)
which will pass the int only there, the function receives the address of the passing argument.Func2(int *a)
- when you call this function withFunc2(&a)
, this statement just passes the reference of 'a
'. In the called function argument '*a
' which will gain the value which is referring that calling function's parameter '&a
'.
精彩评论