|
-
January 15th, 2004, 07:06 PM
#1
reference and const reference
what is the different between them??
thanks for help
regards
SAto
-
January 15th, 2004, 07:44 PM
#2
Reference is actually a hidden pointer to a variable and once initialized, you are not able to change the address it is holding. As for const reference, it is the same as reference with the additional condition that it also disallow you from changing the value it points to.
-
January 15th, 2004, 11:48 PM
#3
As well, anonymous instances will only lookup match with the latter (const) reference.
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
January 16th, 2004, 09:59 AM
#4
Looking at it by example:
Code:
class T;
void f1( T a );
void f2( T& a );
void f3( const T& a );
T b;
f1( b ); // Copy Constructor of T is called to make a copy a of b
f2( b ); // a is just another name for b. Changes to a will reflect on b
f3( b ); // a is just another name for b. f3 is not allowed to change a/b.
const T c;
f1( c ); // s.a.
f2( c ); // Compiler error. As c is const it cannot be passed to f2.
f3( c ); // OK
-
January 16th, 2004, 10:45 AM
#5
reference: at the conceptual level, a pass-by-reference means you are passing the actual variable. Thus changes to the variable inside the function "come out", that is the value of the argument in the call is changed also.
const reference: same as above, except that the compiler will detect and prevent a change to the const argument inside the function.
Consider this function:
void one(const int &x) {
x = 10;
}
The VC++ compiler flags the assignment line with:
l-value specifies const object
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
|