CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 5 of 5

Thread: reference and const reference

  1. #1
    Join Date
    Sep 2003

    reference and const reference

    what is the different between them??

    thanks for help


  2. #2
    Join Date
    Oct 2002
    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.

  3. #3
    Join Date
    Sep 2002
    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

  4. #4
    Join Date
    Jan 2004
    Düsseldorf, Germany
    Looking at it by example:

    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

  5. #5
    Join Date
    Jan 2004
    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)