Thread View: net.lang.c++
3 messages
3 total messages
Started by gaulke
Wed, 01 Oct 1986 15:00
derived classes/nonmember functions
Author: gaulke
Date: Wed, 01 Oct 1986 15:00
Date: Wed, 01 Oct 1986 15:00
35 lines
1355 bytes
1355 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?
Dave Gaulke
gaulke@a.cs.uiuc.edu
...!{cmcl2,seismo,ihnp4}!uiucdcs!gaulke
Re: derived classes/nonmember functions
Author: Jonathan P. Leech
Date: Fri, 03 Oct 1986 02:51
Date: Fri, 03 Oct 1986 02:51
26 lines
1443 bytes
1443 bytes
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
__@/
Re: derived classes/nonmember functions
Author: 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 );
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads