Unannotated abstract classes

Jonathan M Davis jmdavisProg at gmx.com
Wed Aug 17 22:23:04 PDT 2011


On Thursday, August 18, 2011 08:39:04 Mariusz Gliwiński wrote:
> <code>
> abstract class Parent {
> 	abstract void method();
> }
> 
> class Child : Parent {}
> 
> void main(string[] args) {
> 	new Child;
> }
> </code>
> 
> <output>
> src/test.d(10): Error: cannot create instance of abstract class Child
> src/test.d(10): Error: function method is abstract
> </output>
> 
> Is this really what should happen? AFAIK error should be recognized when not
> overwriting abstract method in non-abstract class.
> 
> It reminds me C++, but i don't really see a point in doing like that. Don't
> you think it's a flaw?
> 
> By saying "Classes become abstract if they are defined within an abstract
> attribute, *or if any of the virtual member functions within it are declared
> as abstract*." you let confusing situations happen. If i wouldn't
> instantiate my Child class, i could easily think, that my Child class is
> not-abstract, ship library, and have a problem.
> 
> Well, I'm almost sure there *is* a reason why both languages are designed
> like that, but it would be great to know it.

I'm afraid that I don't quite get what you're trying to say. For a class to be 
instantiated it cannot have an abstract functions. Parent has abstract 
functions - namely the method function. So, you can't instantiate an instance 
of Parent. Child is derived from parent. It has no functions of its own, but 
because it is derived from Parent, it has Parent's functions - which include 
the method function, which is abstract. Child fails to override method, so 
method is still abstract in Child. As such, Child is abstract and cannot be 
instantiated. So, what's the problem?

Now, I would argue that it should probably be required that Child either 
override all of the abstract functions in its base classes so that it isn't 
abstract or that it be marked with abstract, but unfortunately, it looks like 
that's not required. It would probably be a fairly simple change to the 
language which wouldn't really break backwards compability. So, an enhancement 
request should probably be opened about that if it doesn't already exist.

Is the fact that Child isn't required to be marked with abstract what your 
complaint is, or are you trying to say something else entirely.

- Jonathan M Davis


More information about the Digitalmars-d mailing list