![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
I was told that C++ does not allow 2 references refer to the same object. So I wroteI was told that C++ does not allow 2 references refer to the same object. So I wrote a snippet to verify it:
Code:
Complier reports error: Quote:
But then I wrote another snippet: Code:
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
|
|||
|
|||
Re: I was told that C++ does not allow 2 references refer to the same object. So I wrQuote:
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:
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:
Quote:
Quote:
CPP / C++ / C Code:
Here's the output Code:
Now, here's yours, but I put in a few more print statements and a comment or two: CPP / C++ / C Code:
Output (consistent with your results): Code:
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 GIDBlog
NARMY by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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