Thread: Simple code: Can 2 references refer to the same object?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Simple code: Can 2 references refer to the same object?

    I was told that C++ does not allow 2 references refer to the same object. So I wrote a snippet to verify it:

    Code:
    class Base{                                                                                                                                                                  
        string& s;                                                                                                                                                               
    public:                                                                                                                                                                      
        Base(string a):s(a){}                                                                                                                                                    
        ~Base(){cout<<"Base"<<endl;}                                                                                                                                             
    };                                                                                                                                                                           
                                                                                                                                                                                 
                                                                                                                                                                                 
    int main(int argc, char* argv[]){  
        string b = "asdfa";  
        string a="abc";
        Base x(a);
        Base y(b);
        y = x;
    }
    Complier reports error:
    main.cc: In member function `Base& Base:perator=(const Base&)':
    main.cc:30: error: non-static reference member `std::string&Base::s', can't use
    default assignment operator
    This is what I expected: 2 references CAN NOT refer to the same object.

    But then I wrote another snippet:

    Code:
        string a = "abc";                                                                                                                                                        
        string b = "asdfa";                                                                                                                                                      
        string& x = a;                                                                                                                                                           
        string& y = x;                                                                                                                                                           
        y=x;                                                                                                                                                                     
        cout<<y<<endl;                                                                                                                                                           
        x[0]='X';                                                                                                                                                                
        cout<<y<<endl;
    It seems that this code DOES work. and both x and y refer to the same string. As you modified x to "Xbc", y's value is also changed. How come?!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by meili100 View Post
    It seems that this code DOES work. and both x and y refer to the same string. As you modified x to "Xbc", y's value is also changed. How come?!
    We're talking about references here.
    You are never modifying x at all. You are modifying a.
    Similarly, setting a reference to x isn't assigning a reference to x, but to a.
    References are transparent and will apply anything you do it to the variable it is bound to.

    The first example stems from an entirely different problem.
    You are initializing a member variable, s, with the passed string a (which is temporary, btw, so it should be illegal or produce unexpected results).
    Further, I believe the error is due to your reference member. References cannot be assigned, so a default assignment operator could not be created (since it would make a shallow copy).
    Last edited by Elysia; 05-14-2008 at 06:08 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Who says you can't have 2 references pointing to the same object?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Two references can point to the same object.

    A reference cannot point to two different objects, that might be what you were told.

    That is why the first example fails. When you assign the Base object called x to the base object called y, the compiler generated copy assignment operator is used. That does a member-wise copy. But for a reference, you cannot re-assign it to another object, so the reference member of y cannot have another object assigned to it. That's why you get an error about the default assignment operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM