Virtual function: Difference between revisions

Content deleted Content added
Reluctantly (and after trying to fix it myself), remove new section: unreferenced, bad markup, hard to read (partly because of grammar but also because of unclear writing). Should make clear it's C++ specific. Code example references Microsoft Visual C++ specific warnings/errors without explanation.
Line 112:
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's class is called.<ref>{{cite web |title=Never Call Virtual Functions during Construction or Destruction |last=Meyers|first=Scott|date=June 6, 2005 |url=http://www.artima.com/cppsource/nevercall.html}}</ref> If that function is a pure function, then [[undefined behavior]] occurs.<ref>{{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++|at=§13.4}}</ref>
 
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.