GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-May-2008, 17:49
warrener warrener is offline
New Member
 
Join Date: Mar 2008
Posts: 15
warrener is on a distinguished road

I was told that C++ does not allow 2 references refer to the same object. So I wrote


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: S 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:
Quote:
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  
Old 14-May-2008, 22:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: I was told that C++ does not allow 2 references refer to the same object. So I wr


Quote:
Originally Posted by warrener
I was told that C++ does not allow 2 references refer to the same object.
I don't know who told you that, but it is incorrect. Really.

There is nothing in the C++ language specification that prohibits defining two reference variables that are initialized to the same object. Of course, once they are "bound," to an object, they can't be "unbound" or "rebound."

Now, whether it makes sense to do it or whether it would be considered Good Programming Practice to do it or whether I might do it is something else. In fact the language lets you do lots of things that are sillier than this. Perfectly legal whether you see any good use for it or not.


A reference variable can be thought of as the "name" of an object.

Consider the following: Suppose my mother's name is "Edith."

That's what my dad calls her. (Her "real" name.)
I call her "ma." (That's my name for her).
My brother calls her "mater." (That's his name for her.)

Now in our immediate family (that is in the context of my brother and me and our father and our mother): there are no other Ediths. No other mothers. No other "mas". No other "maters."

On Mother's day, a deliveryman arrives with some flowers and a card addressed to "Edith."

There is also a bottle of cheap perfume and a card addressed to "mater."

There is also a brand new copy of her favorite CD ("We're Not Gonna Take It" by Twisted Sister) to replace the old one that was damaged at the Saint Patrick's Day party this year. This was addressed to "ma."

They all got to the correct family member even though there were several different names.

Consider:

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a = "abcd";
    string &ref1 = a; // ref1 can be considered a "name" for string a
    string &ref2 = a; // ref2 can be considered a "name" for string a
    cout << "Initially:" << endl;
    cout << "       a = " << a << endl;
    cout << "    ref1 = " << ref1 << endl;
    cout << "    ref2 = " << ref2 << endl;
    cout << endl;

    ref1[0] = '1'; // changes a[0]
    cout << "After assignment ref1[0] = '1':" << endl;
    cout << "       a = " << a << endl;
    cout << "    ref1 = " << ref1 << endl;
    cout << "    ref2 = " << ref2 << endl;
    cout << endl;

    ref2[1] = '2'; // changes a[1]
    cout << "After assignment ref2[1] = '2':" << endl;
    cout << "       a = " << a << endl;
    cout << "    ref1 = " << ref1 << endl;
    cout << "    ref2 = " << ref2 << endl;
    cout << endl;

    a[2] = '3'; // changes a[2]
    cout << "After assignment a[2] = '3':" << endl;
    cout << "       a = " << a << endl;
    cout << "    ref1 = " << ref1 << endl;
    cout << "    ref2 = " << ref2 << endl;
    cout << endl;

    return 0;
}

Variables ref1 and ref2 can be considered alternative names for string a. Anything you do to ref1 is actually done to a. Anything you do to ref2 is actually done to a.

Here's the output:
Code:
Initially: a = abcd ref1 = abcd ref2 = abcd After assignment ref1[0] = '1': a = 1bcd ref1 = 1bcd ref2 = 1bcd After assignment ref2[1] = '2': a = 12cd ref1 = 12cd ref2 = 12cd After assignment a[2] = '3': a = 123d ref1 = 123d ref2 = 123d
Quote:
Originally Posted by warrener
So I wrote a snippet to verify it:...

This is what I expected: 2 references CAN NOT refer to the same object.
That's funny, I would have expected "main.cc:30: error: non-static reference member `std::string&Base::s', can't use default assignment operator" Oh, well...
Quote:
Originally Posted by warrener
...another snippet...
Let me show you mine first:
CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a = "abc";
    string b = "asdfa";
    string & x = a; // x can be considered a "name" for a
    string & y = b; // y can be considered a "name" for b

    cout << "Initially:" << endl;
    cout << "        a = " << a << endl;
    cout << "        b = " << b << endl;
    cout << "        x = " << x << endl;
    cout << "        y = " << y << endl;
    cout << endl;

    y = x;          // This assigns a to b, same as if we had written b = a;
                    // Note that "b" and "a" are different objects that
                    // now have the same value
    cout << "After assignment y = x:" << endl;
    cout << "        a = " << a << endl;
    cout << "        b = " << b << endl;
    cout << "        x = " << x << endl; // x still refers to a
    cout << "        y = " << y << endl; // y still refers to b
    cout << endl;

    x[0] = 'X';    // This changes a[0]
    cout << "After assignment x[0] = X:" << endl;
    cout << "        a = " << a << endl;
    cout << "        b = " << b << endl;
    cout << "        x = " << x << endl; // x still refers to a
    cout << "        y = " << y << endl; // y still refers to b

    return 0;
}

Here's the output
Code:
Initially: a = abc b = asdfa x = abc y = asdfa After assignment y = x: a = abc b = abc x = abc y = abc After assignment x[0] = X: a = Xbc b = abc x = Xbc y = abc
Two different references to two different strings. Changing one does not affect the other

Now, here's yours, but I put in a few more print statements and a comment or two:
CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a = "abc";
    string b = "asdfa"; // Not used, but you had it here, so...
    string & x = a; // x can be considered a "name" for a
    string & y = x; // Since x is a "name" for a, now y is a "name" for a
    // Note that y is not a "reference to a reference." It is a
    // reference to a string. x and y refer to the same object, the string a

    cout << "Initially:" << endl;
    cout << "        a = " << a << endl;
    cout << "        b = " << b << endl;
    cout << "        x = " << x << endl;
    cout << "        y = " << y << endl;
    cout << endl;

    y = x;          // This is superfluous: y and x are both names for a
                    // so it is the same as if we had written a = a;

    cout << "After assignment y = x:" << endl;
    cout << "        a = " << a << endl;
    cout << "        b = " << b << endl;
    cout << "        x = " << x << endl; // x still refers to a
    cout << "        y = " << y << endl; // y still refers to a
    cout << endl;

    x[0] = 'X';    // This changes a[0]
    cout << "After assignment x[0] = X:" << endl;
    cout << "        a = " << a << endl;
    cout << "        b = " << b << endl;
    cout << "        x = " << x << endl; // x still refers to a
    cout << "        y = " << y << endl; // y still refers to a

    return 0;
}

Output (consistent with your results):
Code:
Initially: a = abc b = asdfa x = abc y = abc After assignment y = x: a = abc b = asdfa x = abc y = abc After assignment x[0] = X: a = Xbc b = asdfa x = Xbc y = Xbc

Now, x and y refer to the same string. The "assignment "y = x" does nothing, since they refer to the same object.

Since x and y refer to the same object, what happens to a is shown in both x and y. What happens to y is shown in both x and a. What happens to x is shown in both a and y.

Regards,

Dave

Footnote: I have seen it said (more than once, and by some very smart people) that reference variables are kind of like pointers in their functionality, but they are easier to use and less confusing to learn. Well, like all such statements, I would have to say that it depends...

I mean, there are several advantages to reference variables over pointers for many applications, but easy? Well, that depends. (But I said that already.)

For experienced C programmers, pointers are really easy. For people whose C++ instructors aren't very good (or very experienced) C programmers, pointers are probably very mysterious.

Don't get me started...
Last edited by davekw7x : 14-May-2008 at 23:40.
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner working with classes Timothy_Bennett CPP / C++ Forum 14 05-Apr-2007 22:22
constructors/classes mapes479 CPP / C++ Forum 3 19-Nov-2006 17:34
Please Help Me To Build My Calendar!!! suriacute85 Java Forum 0 05-Oct-2006 19:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.