Reference cannot be made to refer to a different variable

Discussion in 'C++' started by amit, Nov 28, 2008.

  1. amit

    amit Guest

    a trivial question. Just want your opinion if my understanding is
    correct...

    class A
    {
    int i;
    public:
    A(int temp):i(temp){ }
    void show() { cout <<"value is : "<<i<<"\n";}
    };
    int main()
    {
    A a1(5);
    A &objref = a1;

    cout <<"Address before assignment.....\n";
    objref.show();
    cout <<"&a1 : "<<&a1 <<" ,&objref : " << &objref <<"\n";

    A a2(12);
    objref = a2; //planning/hoping/expecting to refer to different
    object.

    cout <<"Address AFTER assignment!!!\n";
    objref.show();
    cout <<"&a1 : "<<&a1 <<" ,&objref : " << &objref <<" ,&a2 :
    "<<&a2 <<"\n";
    return 0;
    }


    Output
    ===========
    Address before assignment.....
    value is : 5
    &a1 : 0xbfff9fe0 ,&objref : 0xbfff9fe0

    Address AFTER assignment!!!
    value is : 12
    &a1 : 0xbfff9fe0 ,&objref : 0xbfff9fe0 ,&a2 : 0xbfff9fdc



    Observation
    ============
    The default “operator =” function provided by the compiler is called
    and (a1/objref) gets a copy of the values of a2.
    objref still refers to the same object as a1.

    Question
    ========
    Am I right.
     
    amit, Nov 28, 2008
    #1
    1. Advertising

  2. amit

    Fred Zwarts Guest

    > "amit" <> wrote in message news:...
    > a trivial question. Just want your opinion if my understanding is
    > correct...
    >
    > class A
    > {
    > int i;
    > public:
    > A(int temp):i(temp){ }
    > void show() { cout <<"value is : "<<i<<"\n";}
    > };
    > int main()
    > {
    > A a1(5);
    > A &objref = a1;
    >
    > cout <<"Address before assignment.....\n";
    > objref.show();
    > cout <<"&a1 : "<<&a1 <<" ,&objref : " << &objref <<"\n";
    >
    > A a2(12);
    > objref = a2; //planning/hoping/expecting to refer to different
    > object.
    >
    > cout <<"Address AFTER assignment!!!\n";
    > objref.show();
    > cout <<"&a1 : "<<&a1 <<" ,&objref : " << &objref <<" ,&a2 :
    > "<<&a2 <<"\n";
    > return 0;
    > }
    >
    >
    > Output
    > ===========
    > Address before assignment.....
    > value is : 5
    > &a1 : 0xbfff9fe0 ,&objref : 0xbfff9fe0
    >
    > Address AFTER assignment!!!
    > value is : 12
    > &a1 : 0xbfff9fe0 ,&objref : 0xbfff9fe0 ,&a2 : 0xbfff9fdc
    >
    >
    >
    > Observation
    > ============
    > The default “operator =” function provided by the compiler is called
    > and (a1/objref) gets a copy of the values of a2.
    > objref still refers to the same object as a1.
    >
    > Question
    > ========
    > Am I right.


    Yes, correct. That is where references are for.
    objref will always refer to the same object as a1, that is the definition of objref.
    If you want to manipulate addresses, use pointers instead of references.
     
    Fred Zwarts, Nov 28, 2008
    #2
    1. Advertising

  3. amit

    amit Guest

    On Nov 28, 1:05 pm, Paavo Helde <> wrote:
    > amit <> kirjutas:
    >
    > > a trivial question. Just want your opinion if my understanding is
    > > correct...

    >
    > Yes, references cannot be reseated, that's a good thing about them. Also,
    > they are not objects in C++ sense, meaning that you cannot legally obtain
    > the address or size of the reference itself.
    >
    > If you want reseating you have to use pointers.
    >
    > Paavo


    paavo,
    I am not sure if I agree. I got slightly confused.
    A reference is like a second name of the same object.
    when I do something like:
    A &objref = a1,

    it means the object has 2 names a1 and objref.
    So i should be allowd to get the address of objref and I should also
    be allowed to find the size and both should be same as a1.

    Fo example: I have 2 names, one my official name in official records
    and one my near and dear once call me. I will turn around if you call
    me by either of the names...
     
    amit, Nov 28, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Naman Sud
    Replies:
    0
    Views:
    285
    Naman Sud
    Nov 13, 2003
  2. DJ Dev
    Replies:
    3
    Views:
    16,138
    Gandalf
    Feb 8, 2004
  3. moondaddy
    Replies:
    1
    Views:
    1,789
    moondaddy
    May 4, 2004
  4. Joe Au

    cannot refer to data table

    Joe Au, Aug 19, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    303
    Joe Au
    Aug 20, 2004
  5. tsaprout
    Replies:
    0
    Views:
    463
    tsaprout
    Jul 30, 2008
Loading...

Share This Page