Topic: Why allow virtual function calls in constructors?
Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 1995/10/26 Raw View
keagchon@mehta.anu.edu.au (Geoff Keating 9205156) writes:
>The draft WP says that [section class.cdtor:3]
> When a virtual function is called directly or indirectly from a con-
> structor (including from its ctor-initializer) or from a destructor,
> and the object to which the call applies is the object under construc-
> tion or destruction, the function called is the one defined in the
> constructor or destructor's own class or in one of its bases, but not
> a function overriding it in a class derived from the constructor or
> destructor's class, or overriding it in one of the other base classes
> of the complete object (_intro.object_).
>Why allow this, rather than refusing calls to virtual functions at
>all? It would be nice to say that `if I override this function, then
>the old version will _never_ be called on objects of this type'.
In general the compiler CANNOT diagnose a call to a virtual function
from a constructor or destructor - the call might very well come from
a non-virtual function in a separate compilation unit.
In view of this, the specification in the standard that the virtual
function call be treated as if the actual object were of the type of
the constructor or desctructor currently being executed is certainly
within reason.
--
** The Klingons' favorite food was named by the first earthling to see it **
David Sachs - Fermilab, HPPC MS369 - P. O. Box 500 - Batavia, IL 60510
Voice: 1 708 840 3942 Deparment Fax: 1 708 840 3785
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Author: keagchon@mehta.anu.edu.au (Geoff Keating 9205156)
Date: 1995/10/25 Raw View
The draft WP says that [section class.cdtor:3]
When a virtual function is called directly or indirectly from a con-
structor (including from its ctor-initializer) or from a destructor,
and the object to which the call applies is the object under construc-
tion or destruction, the function called is the one defined in the
constructor or destructor's own class or in one of its bases, but not
a function overriding it in a class derived from the constructor or
destructor's class, or overriding it in one of the other base classes
of the complete object (_intro.object_).
Why allow this, rather than refusing calls to virtual functions at
all? It would be nice to say that `if I override this function, then
the old version will _never_ be called on objects of this type'.
--
-Geoff Keating (keagchon@mehta.anu.edu.au)
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]