Article View: net.lang.c++
Article #333Re: derived classes/nonmember functions
From: Andrew Koenig
Date: Fri, 03 Oct 1986 11:59
Date: Fri, 03 Oct 1986 11:59
69 lines
2448 bytes
2448 bytes
>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 );
}
Technical Details
Message-ID:
<6145@alice.uUCp>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!caip!princeton!allegra!alice!arkReferences:
<66700004@uiucdcsp>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!caip!princeton!allegra!alice!ark From: ark@alice.UucP (Andrew Koenig) Newsgroups: net.lang.c++ Subject: Re: derived classes/nonmember functions Message-ID: <6145@alice.uUCp> Date: Fri, 3-Oct-86 07:59:42 EDT Article-I.D.: alice.6145 Posted: Fri Oct 3 07:59:42 1986 Date-Received: Sat, 4-Oct-86 12:08:04 EDT References: <66700004@uiucdcsp> Organization: Bell Labs, Murray Hill Lines: 69