[Issue 5056] Warning against virtual method call from constructor

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Oct 16 09:51:00 PDT 2010


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


Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com


--- Comment #3 from Steven Schveighoffer <schveiguy at yahoo.com> 2010-10-16 09:50:22 PDT ---
(In reply to comment #1)
> It should be noted that unlike C++ this isn't necessarily a problem in D. In
> C++, each class in the class's hierarchy is constructed one by one until the
> most derived class constructor is called. So, if you call a virtual function in
> a constructor, the object is not in a safe state and won't even correctly know
> what type it is.

In C++, calling a virtual function from a constructor calls that class' version
of the virtual function directly.  In other words, virtual functions are not
virtual inside the constructor.

In D, the virtual function is called, so it could potentially be a problem. 
However, it's also possible in D (unlike in C++) to initialize data before
calling the parent constructor through the use of super.  But of course, the
base class has no say in whether the derived class's version is safe to call. 
So yes, I think it would be a good thing to suggest as a "best practice" 
(certainly not a compiler error or warning), but there are ways to make it
safe.

-- 
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