Topic: Forward declarations like "class Derived : public Base;" might be useful
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1998/03/03 Raw View
Kevin Buhr wrote:
> ...
> why not allow forward declarations like:
>
> class rover : public dog;
>
> to define an incomplete class type "rover" but establish its
> (complete) base class "dog" early so it can be used in the return type
> of overriding functions, like in line 14?
>
> Is this just too silly?
I rather like the idea. I came to the same conclusion about a year
ago, when we were first putting together a large system. We don't
use much inheritance, but where we do we use several levels in
the inheritance hierarchy.
It's generally recognized that using a forward declaration of a
class type, instead of #including the header file for the class,
can improve compilation times noticeably. For example:
class Foo;
is faster than:
#include "foo.h"
if all you want type Foo for is for pointers and references, as in:
class Bar
{
Foo * member;
};
It seems completely reasonable to allow:
class Der: public Base;
for the same situations. In this case, you need to let the compiler
know that Der is derived from Base, but nothing else, since you only
intend to use pointers and references to Der and Bar. There
shouldn't be a need to include the whole declaration of Der.
The only hitch that comes to mind is, how does the compiler ensure
that Der really is derived from Base? What kinds of problems
will this create if it isn't? For example, what if I had used
this forward declaration instead?:
class Der: public PhonyBase;
-- David R. Tribble, david.tribble@noSPAM.central.beasys.com --
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Anatoli <REMOVETHISanatoli@ptc.com>
Date: 1998/03/04 Raw View
David R Tribble wrote:
> [snip]
> It seems completely reasonable to allow:
>
> class Der: public Base;
>
> for the same situations. In this case, you need to let the compiler
> know that Der is derived from Base, but nothing else, since you only
> intend to use pointers and references to Der and Bar. There
> shouldn't be a need to include the whole declaration of Der.
In most implementations, conversion from Der* to Base* may involve
some hidden pointer arithmetic, especially when multiple inheritance
is present. Consequently, in order to generate code for such
conversion,
the compiler need to see the full text of both Der and Base.
So such forvard declarations are virtually useless. Therefore
they are not in the language.
The only (?) case when you want forward inheritance declaration and
do not want pointer conversion is declaring (not defining)
mutually covariant classes, like in:
class A;
class B;
class A
{
B * foo ();
};
class B
{
A * bar ();
}
class AA : public A
{
BB * foo (); //oops -- we don't know yet that
//BB is derived from B
//`class BB : public B;' might help here
}
class BB : public B
{
AA * bar ();
}
>
> The only hitch that comes to mind is, how does the compiler ensure
> that Der really is derived from Base? What kinds of problems
> will this create if it isn't? For example, what if I had used
> this forward declaration instead?:
>
This is not a problem since non-diagnostics of several types of
errors is already allowed.
[To mod: shouldn't it be in the FAQ?]
Regards
--
Anatoli (anatoli at ptc dot com)
opinions aren't
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Garth A. Dickie" <dickie@ra.avid.com>
Date: 1998/03/04 Raw View
David R Tribble wrote:
> It seems completely reasonable to allow:
> class Der: public Base;
>
> [ ... ] In this case, you need to let the compiler know that Der
> is derived from Base, but nothing else, since you only intend to
> use pointers and references to Der and Base. There shouldn't be
> a need to include the whole declaration of Der.
I think that the biggest technical problem is that you cannot in general
convert a pointer to Der to a pointer to Base without knowing the
layout of the Der object. For example, Der might have several
private base classes.
Regards,
Garth
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: christian.bau@isltd.insignia.com (Christian Bau)
Date: 1998/03/06 Raw View
In article <34FD6027.2F2B@elastic.avid.com>, dickie@ra.avid.com wrote:
> David R Tribble wrote:
> > It seems completely reasonable to allow:
> > class Der: public Base;
> >
> > [ ... ] In this case, you need to let the compiler know that Der
> > is derived from Base, but nothing else, since you only intend to
> > use pointers and references to Der and Base. There shouldn't be
> > a need to include the whole declaration of Der.
>
> I think that the biggest technical problem is that you cannot in general
> convert a pointer to Der to a pointer to Base without knowing the
> layout of the Der object. For example, Der might have several
> private base classes.
I think an incomplete declaration
class Der : public Base;
should have to mean that this is really how it is derived, so writing
later (or earlier)
class Der : public Base, Base2 { int x; };
would be an error that the compiler must find. I think things like
class Der : private Base { int x };
or
class Intermediate : public Base { int xi; };
class Der : public Intermediate { int x; }
would have to be errors as well.
I think it is always be possible for a compiler to cast from a pointer to
a derived class to pointer to any base class, knowing only from which base
classes the derived class is derived and in which order and the layout of
all base classes.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: buhr@stat.wisc.edu (Kevin Buhr)
Date: 1998/02/25 Raw View
Clause 10.3.5 of the second committee draft requires that, if the
return type of a virtual member function D::f differs from B::f (where
D is derived from B, of course), the class type in the return type
must be complete at the declaration of D::f (or else actually be D).
I presume the rationale is that, in code like the following:
1 class dog;
2 class dog_food {
3 public:
4 virtual dog* eater();
5 };
6 class dog {
7 public:
8 virtual dog_food* meal();
9 };
10
11 class rover;
12 class rover_food : public dog_food {
13 public:
14 rover* eater(); // ERROR: "rover" is incomplete
15 };
16 class rover : public dog {
17 public:
18 rover_food* meal(); // OK: "rover_food" is complete
19 }; // and derives from "dog_food".
it places a burden on the compiler to remember that the validity of
the declaration in line 14 depends on the fact that, in line 16,
"rover" is defined as a derived class of "dog".
However, this code snippet gives an example of a fairly reasonable
paradigm. We have two parallel hierarchies of cooperating classes
where the two base classes can each return pointers to the other base
class. Yet, we'd like to write code that takes advantage of the fact
the derived classes each always return pointers to the other *derived*
class.
I guess I'm wondering if (1) I'm missing a way of accomplishing this
cleanly (I can think of several "unclean" ways); and (2) if not, why
not allow forward declarations like:
11 class rover : public dog;
to define an incomplete class type "rover" but establish its
(complete) base class "dog" early so it can be used in the return type
of overriding functions, like in line 14?
Is this just too silly?
Kevin <buhr@stat.wisc.edu>
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]