Content deleted Content added
+most important statement |
virtual method example |
||
Line 109:
Pure virtual functions can also be used where the method declarations are being used to define an [[interface (Java)|interface]] - similar to what the interface keyword in Java explicitly specifies. In such a use, derived classes will supply all implementations. In such a [[design pattern]], the abstract class which serves as an interface will contain ''only'' pure virtual functions, but no data members or ordinary methods. In C++, using such purely abstract classes as interfaces works because C++ supports [[multiple inheritance]]. However, because many OOP languages do not support multiple inheritance, they often provide a separate interface mechanism. An example is the [[Java (programming language)|Java programming language]].
==
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 base class constructors is fatal in C++ (and compilers or linker would not issue error nor warning according to the scope of the language), therefore being generally discouraged (softly speaking).<syntaxhighlight lang="c++">
struct Base {
virtual void test1() = 0;
void base_func() {
test1();
}
// warning C5204, care about virtual destructors? - read bellow
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.▼
virtual ~Base() {
}
};
struct Mid : Base {
virtual void test2() = 0;
Mid() {
//test1(); // linker LNK2019, unresolved symbol
base_func(); // compiles all good - fatal runtime (on construct)
}
};
struct Derived : Mid {
void test1() {
}
void test2() {
}
};
int main() {
Derived d;
}
▲</syntaxhighlight>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.
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.
|