|
-
November 1st, 2002, 04:32 AM
#4
Originally posted by Yves M
Well, yes that works, but obviously you won't be able to change a_i, which might defeat the point of passing by reference.
But there is no solution to this (as far as I can imagine), since the reference operator implies that you are actually passing a dereferenced pointer to an int. This means that a_i has to be somewhere in memory as opposed to a constant like -1.
The reason it works with const int& a_i = -1 is that the compiler will allocate a static variable for -1 and &a_i will point to that one. So it's the same as writing :
Code:
const static minusone = -1;
void foo(const int &i = minusone)
{
// code
}
The reason for passing reference in such case is to eliminate the need to write a copy constructor (if that needs to be). The const keyword is to tell the user of your function that the object he passed, wouldn't get modifed.
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
|