[net.lang.c++] derived classes/nonmember functions

gaulke@uiucdcsp.cs.uiuc.edu (10/01/86)

I am having a problem with nonmember functions and derived classes.
Specifically, if I define a nonmember function taking a base class as
an argument, then C++ (version 1.0) does not allow me to call the same
function with a derived class as an argument.  The following example
illustrates the situation:
	class X {
		int i;
	public:
		friend int inc( X );
		X()	{i=0;}
	};

	class Y : X {
	};

	int inc( X x ) {
		return x.i + 1;
	};

	main() {
		Y y;
		inc( y );
	}

When compiled, it gives this error message:
	line 17: error: cannot make a X from a Y 

But it should be real easy to "make a X from a Y", since any Y is also an
X.  Is there a reason why this isn't allowed? Is it possible to allow it?

				Dave Gaulke

				gaulke@a.cs.uiuc.edu
				...!{cmcl2,seismo,ihnp4}!uiucdcs!gaulke

jon@cit-vax.Caltech.Edu (Jonathan P. Leech) (10/03/86)

Summary:
Expires:
Sender:
Followup-To:
Distribution:

Organization : California Institute of Technology
Keywords:

In article <66700004@uiucdcsp> gaulke@uiucdcsp.cs.uiuc.edu writes:
>
>I am having a problem with nonmember functions and derived classes.
>Specifically, if I define a nonmember function taking a base class as
>an argument, then C++ (version 1.0) does not allow me to call the same
>function with a derived class as an argument.	The following example
>illustrates the situation:
> ...

    There is at least one good reason this won't work: a derived class
may be larger than its base class. The compiler will calculate the
wrong frame offsets for any other function parameters and possibly for
the class argument as well.
-- 
    Jon Leech (jon@csvax.caltech.edu || ...seismo!cit-vax!jon)
    Caltech Computer Science Graphics Group
    __@/

ark@alice.UucP (Andrew Koenig) (10/03/86)

>I am having a problem with nonmember functions and derived classes.
>Specifically, if I define a nonmember function taking a base class as
>an argument, then C++ (version 1.0) does not allow me to call the same
>function with a derived class as an argument.  The following example
>illustrates the situation:
>	class X {
>		int i;
>	public:
>		friend int inc( X );
>		X()	{i=0;}
>	};
>
>	class Y : X {
>	};
>
>	int inc( X x ) {
>		return x.i + 1;
>	};
>
>	main() {
>		Y y;
>		inc( y );
>	}
>
>When compiled, it gives this error message:
>	line 17: error: cannot make a X from a Y 
>
>But it should be real easy to "make a X from a Y", since any Y is also an
>X.  Is there a reason why this isn't allowed? Is it possible to allow it?

Actually, there are *two* errors in this program: since X is
a private base class of Y (you said    class Y: X    instead of
class Y: public X), the constructor X() is considered private
by Y member functions.  Since constructing a Y needs to be
able to construct an X, and X() is private to Y, you're in trouble.
(I think 1.0 fails to pick this up and 1.1 catches it).

Anyway, while it is true that you have said that a Y is just like an
X, it is not automatically possible to treat a Y as an X.  Reasons:

	1. You can use a Y as if it were an X and you can use a
	pointer to a Y as if it were a pointer to an X.  This does
	not mean, however, that you can assign a Y to an X or
	initialize a Y from an X.  In order to do that, you muse
	define the assignment or initialization operators, or write
	a constructor or conversion operator to change Y to X.


	2. Since you can treat a pointer to a Y as if it were a
	pointer to an X, you can treat references similarly:

		class X {
			int i;
		public:
			friend int inc( X& );
			X()	{i=0;}
		};
		
		class Y : public X {
		};
		
		int inc( X& x ) {
			return x.i + 1;
		};
		
		main() {
			Y y;
			inc( y );
		}