[Issue 5056] Warning against virtual method call from constructor

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jun 5 08:16:47 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=5056



--- Comment #13 from Michal Minich <michal.minich at gmail.com> 2013-06-05 08:16:45 PDT ---
(In reply to comment #12)
> (In reply to comment #11)
> > dmd 2.063 - instance method is called before constructor.
> 
> There's a simple fix for this, simply call super() directly whenever you want

Slightly better, but hardly a fix. at best partial workaround. you exchange one
invalid initialization order for other invalid order - now Derived is
initialized before Base. I guess is a less of a problem as developer of Derived
can handle this invalid order locally.

Think of Base and Derived to be developed by independent developers not knowing
the details of implementation:

In current scheme, to ensure that derived class is initialized before any
overridden method can be called: developer has to explicitly call the super()
constructor as the last step in derived constructor. And that must be done for
every derived class on which any method is overridden. Also this would not only
for the most-derived class, if there is more derived classes in hierarchy -
every one hast to do it.

Two sides of problem are:

- Developer of Base might not be aware that method called in constructor is
virtual, so he accidentally allows it to be hijacked.

- Developer of Derived might be not aware that method  he has overridden is
called in Base constructor, which makes his overridden method to be allays
called before his constructor his called.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list