Abstract functions in child classes
Jonathan M Davis
jmdavisProg at gmx.com
Fri Dec 2 14:28:33 PST 2011
On Friday, December 02, 2011 21:13:45 Adam wrote:
> Anyway, I have my answer, and I know that D *does* have a reason for
> this implicitism.
Okay. Maybe I've been skimming over this thread too much, but I don't
understand what forcing the programmer to mark an abstract class as abstract
would do. The compiler knows that the class is abstract regardless. It's going
to generate an error when you try and instantiate that class - whether or not
you mark the class as abstract or not has no impact on that as long as there
are abstract functions (the sole bizareness being able to mark a class as
abstract when none of its functions are abstract).
It is perfectly legal and desirable to be able to have a class reference for
an abstract class. e.g.
abstract class C
{
abstract void func();
}
class D : C
{
void func()
{
writeln("hello world");
}
}
C c = new D();
The compiler has nothing to complain about with a reference to an abstract
class until you try and instatiate it. So, how does explicitly marking the
class abstract help with that?
I do think that it's a bit odd that D doesn't require that you mark classes as
abstract if they have abstract functions and that it allows classes which
don't have abstract functions to be marked abstract, but that should have no
effect on whether the compiler can catch bugs related to abstract classes. The
fact that the class has functions which are abstract is enough for the
compiler to know that the class is abstract.
new C() is what needs to be disallowed for abstract classes, and that is an
instantiation. It's the instantations that need to be checked, and they _are_
checked. It is a compilation error when you try and instantiate an abstract
class.
The only thing that I see that marking the class abstract does is give the
programmer a visual indicator that the class is abstract. I do think that
that's valuable, and I'd love it if it were required when any functions in the
class are abstract and disallowed when none are, but I don't see how it can
have any effect on what errors you're getting. It's instatiating an abstract
class which as error, not declaring one, and unless you actually instantiate
it, there's no way for the compiler to catch it, because there's nothing to
catch.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list