super constructors: I can't take it anymore!

Oskar Linde oskar.lindeREM at OVEgmail.com
Wed Aug 16 04:10:44 PDT 2006


Serg Kovrov wrote:
> Hello, everybody,
> 
> As subject says I am really on the edge. This implicitly generated 
> parent constructor calls are... Evil.
> 
> Here is my near-real class hierarchy:
>> class BaseWindow {
>>   this() {
>>     writefln("register BaseWindowClass");
>>     writefln("create");
>>   }
>> }
>> class FrameWindow : BaseWindow {
>>   this(char[] caption) {
>>     writefln("register FrameWindowClass");
>>     writefln("create %s", caption);
>>   }
>> }
>> class MyFrameWindow : FrameWindow {
>>     this(char[] caption) {
>>         super(caption);
>>     }
>> }
> 
> When I create instance of MyFrameWindow, I've got:
>> register BaseWindowClass
>> create
>> register FrameWindowClass
>> create test
> 
> I think I understand why, but I'm completely unhappy with it =(
> 
> What I want is that the only constructor called were 
> FrameWindow.this(char[]), to have *only* this:
>> register FrameWindowClass
>> create test

If you want to avoid the default constructor to be called in the base 
class, I guess you would have to create a overloaded dummy constructor 
in BaseWindow that you explicitly call in your subclass constructor.

Or, you can reconsider your design. One option is to make a register() 
method that only the BaseWindow constructor calls. In you child classes, 
you can overload the register() method anyway you wish.

/Oskar



More information about the Digitalmars-d-learn mailing list