Topic: forward class declarations including derivation?


Author: sgpalam@canrem.com (Shell Account - Gordon Palameta)
Date: Sun, 29 Nov 1992 06:43:58 GMT
Raw View
Would it make sense to allow derivation information to be included
in a forward class declaration?

For instance:

    class D;

    B *foo()  { D *x; return x; }

will result in an error, whereas the declaration:

    class D : public B;   // not legal

would make it clear that this is OK.


Of course, you presumably don't want to   #include <D.h>, since
that would make the forward declaration unnecessary in the first
place.

--
sgpalam@canrem.com




Author: bs@alice.att.com (Bjarne Stroustrup)
Date: 29 Nov 92 15:17:02 GMT
Raw View

gpalam@canrem.com (Shell Account - Gordon Palameta @ Canada Remote Systems) writes

 > Would it make sense to allow derivation information to be included
 > in a forward class declaration?
 >
 > For instance:
 >
 >     class D;
 >
 >     B *foo()  { D *x; return x; }
 >
 > will result in an error, whereas the declaration:
 >
 >    class D : public B;   // not legal
 >
 > would make it clear that this is OK.
 >
 > Of course, you presumably don't want to   #include <D.h>, since
 > that would make the forward declaration unnecessary in the first
 > place.

Exactly, but what if D was defined

 class D : public A, public B { ... };

Then, naively assuming that the start of the B in D is the same as the
start of the D itself - as a compiler would have to do if it accepted

 class D : public B;   // not legal

would lead to errors.

Further, allowing forward declaration of properties of a class leads to
serious opportunities for confusion and implementation complexity. Checking
the consistency of use of a class that has a variety of partial specifications
in a set of separately compiled and possibly dynamically linked fragments
would be a nightmare.




Author: steve@taumet.com (Steve Clamage)
Date: Sun, 29 Nov 1992 16:58:02 GMT
Raw View
sgpalam@canrem.com (Shell Account - Gordon Palameta) writes:

>Would it make sense to allow derivation information to be included
>in a forward class declaration?

>For instance:

>    class D;

>    B *foo()  { D *x; return x; }

>will result in an error, whereas the declaration:

>    class D : public B;   // not legal

>would make it clear that this is OK.

But it doesn't always help in the presence of multiple inheritance:

 class A, B;
 class D : public A, public B;
 D *dp;
 B *bp = dp; // can't convert the pointer

There are some cases where knowing only the outline of the hierarchy is
helpful.  There are others where it isn't.  Allowing the derivation
specification increases the chance of program error, since the derivation
would be specified in at least two places.  In my view (you might
disagree) the benefits do not outway the disadvantages.

We must also ask what problem this suggestion is attempting to solve.
Let me guess:

1. Including the headers in every module is expensive.
-> Some compilers already have means to minimize the cost.  If yours
   doesn't, lobby your vendor.

2. I want to keep the internals of the classes hidden.
-> There are other ways to do this.  They are discussed in books and have
   been discussed in comp.lang.c++.
--

Steve Clamage, TauMetric Corp, steve@taumet.com
Vice Chair, ANSI C++ Committee, X3J16