Path: utzoo!attcan!uunet!inxsvcs!mwg From: mwg@inxsvcs.UUCP (Phil Blecker) Newsgroups: comp.lang.c++ Subject: Re: A question or two from a Beginner Summary: automatic class creation and value by reference Message-ID: <150@inxsvcs.UUCP> Date: 13 Jan 89 00:39:34 GMT References: <1001@elmgate.UUCP> Reply-To: mwg@inxsvcs.UUCP (Phil Blecker) Organization: INX Services, Los Angeles, CA Lines: 42 >jdg@elmgate.UUCP (Jeff Gortatowsky CUST) writes: >First, the string class shown on page 185 of "The C++ Programming >Langauge". >... Take for example (using that class example) >string foo ("abc"); > if ("ABB" == foo) { ..... etc.... The reason the code is not an error, is because the compiler knows how to create a string object from a character pointer. The result of that code is to create a temporary string object from the pointer to "ABB", do the comparison between the temporary string object and foo, save the result of the comparison and then destroy the temporary string object. If you think people might really do this, it would be better to create an implementation for it, for performance's sake. Yes, the constructors and destructors are called when the temporary object is used. BTW, examples in books are rarely complete. And, yes, it would have complained if it couldn't convert the character pointer to a string or something else that was implemented. I know that in cfront 1.2.1 the constructors and destructors are actually called in the if condition block, before the body of the if executes, but I wouldn't want to rely on every compiler doing that. As for references ... they ARE just pointers. And in most cases I don't like to use them. I prefer to pass a pointer explicitly. Having to put an address-of operator in front of a argument reminds me that it might be changed by the function. About the only time I use them is when I want it to look as though I'm passing a class object without actually having to put anything more than a pointer to the object on the stack. This lets me keep the semantics of using the address-of operator to mean that it might be changed in the function more clear without a lot of overhead. When you declare that a function accepts an argument by reference, the compiler actually passes a pointer to the object to the function. When you access or assign that object in the function, even though you don't show the dereferencing explicitly, the compiler has to dereference the pointer before use. All value by reference does is keep you from having to explicitly take the address and dereference the object. That's done for you by the compiler. -- Phil Blecker +1 818 243-3053 none of my ideas belong to me and uunet!inxsvcs!mwg i can't see anything wrong with that