I'm learning basic C++ and would like to know what the differents are for using passing by reference instead passing by address. Is suppose to be like one of many features that C++ improves on that C had?
I'm learning basic C++ and would like to know what the differents are for using passing by reference instead passing by address. Is suppose to be like one of many features that C++ improves on that C had?
they are both considered "passing by reference" but by the way you worded it I think you mean using references instead of pointers.
A reference is a pointer but works more like an alias instead. You can only set what it points to when you create it and it can't be changed afterwards. Everytime you use it you don't have to explicitly dereference it -- you treate it as though it were an object rather than a pointer to an object.
Another benefit of it is that when you initialize it, you don't explicitly use the address of operator, the address is taken implicitly. Example:
int a;
int& ref = a;
ref = 4;
now 'a' is set to 4.
That also means that when you pass a variable to a function that has a reference as an argument, you don't even have to be concerned (in most cases) whether it takes a reference or an object since you can pass it with the same syntax.
So basically the benefit of a reference is that you can work with the data being pointed to without having to explicitly derefrence it all the time. It makes for less operators and code that's easier to read.
Reference is faster than pointer. In general, use const reference when possible.
Kuphryn
Last edited by kuphryn; 01-08-2003 at 02:49 PM.
I have a question on this subject.
If you have an array of pointers to objects on the free store. You make sure that none of the pointers are NULL and if the address of these objects will never change. Is it ok to pass references to these objects between functions instead of pointers? Would this improve performance any at all or just the readability?
-----------------------
R. D. Nichols, Jr.
-Why do I have to go EVERYWHERE in this handbasket?
Please ignore the performance part of that question, I just noticed the previous reply....
-----------------------
R. D. Nichols, Jr.
-Why do I have to go EVERYWHERE in this handbasket?
That's misinformation. A reference is not faster than a pointer, they are exactly the same in terms of performance.Originally posted by kuphryn
Reference is faster than pointer. In general, use const reference when possible.
Kuphryn
There is literally no performance difference between using a pointer vs a reference -- it's just that a reference is implicitly dereferenced rather than explicitly. There is no such thing as a "const reference." The value of a reference can never change -- in that sense, they are always "constant" and you can never change that. If he meant using a reference to a constant, there will be no performance gain or logical change from using a constant pointer to a constant. References just simplify the use.
"If he meant using a reference to a constant, there will be no performance gain or logical change from using a constant pointer to a constant."
There is indeed a logical difference.
Pointers can be null, references cannot.
References are no slower than pointers, but in some cases, your compiler _MAY_ optomize away references. It's best to forget that you knew this, though there is a nifty trick that lets you turn
a = b+c*d;
into
a.plus_times(b,c,d);
Never use a reference to access something that you can normally only get to with a pointer, the exception being returning *this in a method that returns a reference. Never have references to something on the heap, i.e.
foo &f = *(new foo(x,y));
...
f.no_arrow_for_me();
...
delete &f;
I was refering to a dereferenced constant pointer -- otherwise there are many logical differences.Originally posted by SilentStrike
"If he meant using a reference to a constant, there will be no performance gain or logical change from using a constant pointer to a constant."
There is indeed a logical difference.
Pointers can be null, references cannot.
Huh, Poly? What about this?Originally posted by Polymorphic OOP
There is no such thing as a "const reference." The value of a reference can never change -- in that sense, they are always "constant" and you can never change that. If he meant using a reference to a constant, there will be no performance gain or logical change from using a constant pointer to a constant. References just simplify the use.
That's what I was taught was a constant reference parameter. The advantage to including 'const' is basically that you can't change the value being passed to the function. If one tries to change it, one will get a compiler error. I personally think it also makes the code easier to read, for the const says that that argument won't be changed in that function. I think that's what Kuphryn meant by a 'const reference'.Code:int some_function(const &value);
FAQ
"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.
"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.
Huh, Poly? What about this?
Code:int some_function(const &value);
Nope, thats a reference to a const, not a const reference.
Examples:
int* a; // pointer to an int
const int* a; // pointer to a const int
int* const a; // const pointer to an int
const int* const a;// const pointer to a const int
now with references:
int& a; // reference to an int
const int& a; // reference to a constant int
int& const a; // Nope, nothin, sorry
const int& const a; // Same here
A reference is always constant, but a "const reference" is not defined.
Last edited by Polymorphic OOP; 01-08-2003 at 11:12 PM.
Const reference is pretty popular term, occurs about 16 times more than reference to const.
http://www.google.com/search?hl=en&l...=Google+Search
http://www.google.com/search?hl=en&l...=Google+Search
Just something that many nitpick about...
void f(int* a) { }
Technically is pass by value. In fact, every kind of parameter
passing in c is pass by value except inline functions using
#define. C++ has pass by referance though.
All depends on the concept of the pass.
You can look at your example as passing the value by reference OR you can look at it as passing the pointer by value. Passing by reference doesn't imply actually using a C++ reference type.
It does not imply only because everyone botches
the definition of passing by referance. Even if you
pass an array to a function the array is decayed to
a pointer that is passed by value.
http://www.eskimo.com/~scs/C-faq/q4.11.html