Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Virtual function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Behavior during construction and destruction == Languages differ in their behavior while the [[Constructor (computer science)|constructor]] or [[Destructor (computer science)|destructor]] of an object is running. For this reason, calling virtual functions in constructors is generally discouraged. In C++, the "base" function is called. Specifically, the most derived function that is not more derived than the current constructor or destructor's class is called.<ref name="cpp-std">{{cite web|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf|title=N4659: Working Draft, Standard for Programming Language C++}}</ref>{{rp|Β§15.7.3}}<ref name=chen>{{cite web |title=What is __purecall? |last=Chen|first=Raymond|author-link=Raymond Chen|date=April 28, 2004 |url=https://devblogs.microsoft.com/oldnewthing/20040428-00/?p=39613}}</ref><ref>{{cite web |title=Never Call Virtual Functions during Construction or Destruction |last=Meyers|first=Scott|author-link=Scott Meyers|date=June 6, 2005 |url=http://www.artima.com/cppsource/nevercall.html}}</ref> If that function is a pure virtual function, then [[undefined behavior]] occurs.<ref name="cpp-std"/>{{rp|Β§13.4.6}}<ref name=chen /> This is true even if the class contains an implementation for that pure virtual function, since a call to a pure virtual function must be explicitly qualified.<ref>{{cite web |title=C++ corner case: You can implement pure virtual functions in the base class |last=Chen|first=Raymond|author-link=Raymond Chen|date=October 11, 2013 |url=https://devblogs.microsoft.com/oldnewthing/20131011-00/?p=2953}}</ref> A conforming C++ implementation is not required (and generally not able) to detect indirect calls to pure virtual functions at [[compile time]] or [[link time]]. Some [[runtime system]]s will issue a pure virtual function call error when encountering a call to a pure virtual function at [[Runtime (program lifecycle phase)|run time]]. In Java and C#, the derived implementation is called, but some fields are not yet initialized by the derived constructor (although they are initialized to their default zero values).<ref>{{cite web |title=Joy of Programming: Calling Virtual Functions from Constructors |last=Ganesh|first=S.G.|date=August 1, 2011 |url=https://www.opensourceforu.com/2011/08/joy-of-programming-calling-virtual-functions-from-constructors/}}</ref> Some [[Design pattern (computer science)|design patterns]], such as the [[Abstract Factory Pattern]], actively promote this usage in languages supporting this ability.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)