[comp.lang.c++] A question or two from a Beginner

jdg@elmgate.UUCP (Jeff Gortatowsky CUST) (01/12/89)

Well after reading the available news in this group for the first time
it appears C++ is still in qutie a state of flux.  I'm an 'experianced'
C programmer who has just started to use C++.  My two platforms are
a Sun (G++) and a PC (Zortech).  But my questions are generic.
I apologize if they seem 'dumb'.

First, the string class shown on page 185 of "The C++ Programming
Langauge".  A few pages further on Mr. Stroustrup explains that operators
which may have a non-lvalue operands should be 'friends' of a
class.  This made good sense until I backed up to the friend functions of
the string class.
Take for example (using that class example)
string foo ("abc");
	if ("ABB" == foo) { ..... etc....

Would not the compiler complain in any case that the arguments do not match
either of the two '==' operator functions declared?  Shouldn't there be
another overloaded '==' function that takes a char* first and a string
object reference second?  Is "ABB" automagically converted to a string
object and the 2nd '==' friend function called?  If so then a constructor
would be called, yes?  And right after the if statement the destructor?
Is the example string class complete? Will the compiler reverse the 
two operands?  No it couldn't, could it.  That would mess up operators such
as <= or >.  Did I miss something?

References in C++ really mess me up.  Anyone got a lucid explanation for
a C programmer? Reference to me always meant "address of".

To Mr. Bright.  I had no problems obtaining or using your compiler (1.07).
But then I'm a C++ novice and have no non-trivial code to compile yet.
I'm looking forward to future releases.  Thanks for the work.

Sorry for the length.  Thanks for any helpful answers.

-- 
Jeff Gortatowsky-Eastman Kodak Company  .....rochester!kodak!elmgate!jdg
(use uuhosts or such to find path to rochester)
Eastman Kodak makes film not comments.  Therefore these comments are mine
not theirs.

mwg@inxsvcs.UUCP (Phil Blecker) (01/13/89)

>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

shopiro@alice.UUCP (Jonathan Shopiro) (01/14/89)

Here is a program that should work.

struct X {
	X(char*);
	~X();
	int dummy;
};
int	operator==(const X&, const X&);
int foo(const X& x)
{
	return "abc" == x;
}

If operator==() were a member of X instead of a friend, it would not work,
since no coercion is done on the object for which a member function is
called.

Of course this is not very efficient, because an X object is created and
destroyed within foo.  It would be better to have an additional function

int	operator==(char*, const X&);
-- 
		Jonathan Shopiro
		AT&T Bell Laboratories, Warren, NJ  07060-0908
		research!shopiro   (201) 580-4229