home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: What is referencing good for?
- Message-ID: <marnoldDnJ271.K09@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <4goojd$a9g@wintermute.ecs.fullerton.edu>
- Date: Thu, 29 Feb 1996 07:59:25 GMT
- Sender: marnold@netcom23.netcom.com
-
- grosin@titan.fullerton.edu (Gil Rosin) writes:
-
- >I've been playing with referencing all day, and I can not find one use for it
- >that I can not do with pointers. What is referencing good for? I know about
- >reference parameters and I know about reference functions.
-
- >The only pro I found about reference parameters is that it gives cleaner code,
-
- [code snipped]
-
- >Aside from that, I found referencing to have the same uses as pointers, if I
- >need to pass huge objects I can pass the address instead of the entire data,
- >but I can do this with pointers as well.
-
- >I don't quite understand what reference functions are good for. Again, I can
- >do the same stuff with pointers.
-
- References are functionally equivalent to pointers, they are implemented
- basically as pointers are and, yes, they give cleaner code in situations
- where pointers probably wouldn't have.
-
- One major usage difference between pointers and references is that
- references may only be initialized *once*. Plus, you have to go out of
- your way to initialize a reference to refer to an invalid object. In
- this simply way, references give the functionaity of pointers, but in a
- slightly "safer" way. Plus, they give cleaner code to boot.
-
- Pointers, on the other hand, can be initialized as many times as you like
- (and with as many bogus values as you like). The compiler cannot help you
- to put the right address into a pointer variable (it's perfectly legal C++
- to set a pointer to "null" or some other "non-address" and then try to
- dereference it).
-
- >Can someone give me a REAL world example of where I would use referencing
- >perhaps somewhere that I can not use pointers?
-
- When you want to write code that ensures that once something is set to
- point (refer) to something, is can only *ever* point to that something.
-
- >And keep in mind that I'm a newbie, I'm just trying to learn the basics
- >right now.
-
- It's might be hard to appreciate references until you appreciate all the
- trouble it's possible to get into with pointers. ;-)
-
-
- But, there's much more to it than that...
-
- Reference parameters can be very important to C++ code (which is why they
- were added). Consider, for example, a class with an assigment member (an
- operator= member function). If this member took a non-reference/non-pointer
- parameter...
-
- Object& Object::operator=(Object& obj)
- {
- // copy obj
-
- return *this;
- }
-
- ...the object passed is an "obj" will be passed *by value* every time! What
- if Object's have 5 megabytes of state data? Obviously, you don't want to
- pass such large objects by value, especially when you're going to copy thema
- second time on purpse---you'd rather pass a reference. Or, in your case you'd
- rather pass a pointer. But, no wait! If Object::operator=() were declared
- like this...
-
- Object& Object::operator=(Object* pobj);
-
- ...the you'd have to call it like this...
-
- Object obj1;
- Object obj2;
-
- obj1 = &obj2;
-
- ...which doesn't really "look" like what it does. You want to write
- code that says, "Make obj1 equal to obj2". The code above, taken literally,
- says "Make obj1 equal to the address of obj2", which really doesn't exress
- what is really happening.
-
- You'd rather (I'd hope), want to write...
-
- obj1 = obj2;
-
- ...but now obj2 will be copied onto the stack to call operator=(). What
- to do? Why, use a reference parameter, of course!
-
- C++ references let the details of whether of not something is to be passed
- by, well, reference to be handled by the callee, as opposed to the caller.
- As you can see, in C++, this can be very important. With regular pointer
- parameters, the *caller* always has to know to manually pass an address. In
- C++, this can lead to code that looks a bit funny.
-
- There are many other examples like this in C++ programming where references
- are the correct way to do something. True, you can do it with pointers,
- but the result isn't often what C++ is supposed to be.
-
- Did that help?
-
-
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-