C + + face question 1: Can a virtual function be called in constructors and fictional functions?
- Constructors and imaginary functions can call virtual functions, and the compiler will not give an error.
- In C + + primer It's best not to use
- Because the class is constructed from a base class to a derived class, a virtual function is called in the constructor, and the virtual function does not render a polymorphic
- The destructor of a class is from a derived class to a base class, and when a destructor for a class at a level in the inheritance hierarchy is called, it means that its derived class part has been deconstructed, and therefore does not render polymorphic
- Therefore, if you declare a pure virtual function in a base class and call it in the destructor of the base class, the compiler will get an error.
classbase{ Public: Base () {fuction (); }Virtual voidFuction () {cout<<"Base::fuction"<< Endl; }};classA: Publicbase{ Public: A () {fuction (); }Virtual voidFuction () {cout<<"A::fuction"<< Endl; }};//This defines an object of a, what will be output? A; the first call should be no problem, but the resulting result? A lot of people would say output: a::fuction a::function If the output is based on the above, it means that when the base is constructed, that is, when calling Fuction in the constructor of base, the fuction of subclass A is called. In fact, a has not yet begun to construct, so the behavior of the function is completely unpredictable, so this is obviously not the case, the actual output is: base::fuctiona::fuction
Example 2:
#include <iostream>using namespace STD;classa{ Public: A () {cout<<"a constructor"; Test (); } ~a () {cout<<"a destructor";cout<<"A::test ()"<< Endl; }Virtual voidTest () {cout<<"A::test ()"<< Endl; }};classB: Publica{ Public: B () {cout<<"B Constructor"; Test (); } ~b () {cout<<"B destructor"; Test (); }Virtual voidTest () {cout<<"B::test ()"<< Endl; }};int_tmain (intARGC, _tchar* argv[]) {A * PA =NewB (); Call constructor output A constructor: A::test () B call Constructor B::test ()cout<<"Dynamic invocation:"; Pa->test (); The original pointer type is PA, the actual pointer type is B, because it is a virtual function, so call b::test by actual type (DeletePA; Because the destructor of a is not a virtual function, it is called according to the original type pointer, if the destructor of a is addedVirtualThe output is a B destructor b::test a destructor a::testreturn 0;}
Example 3:
#include<iostream>using namespace STD;classA Public:void VirtualF () {cout<<"A"<<endl; } };classD | PublicA Public:void VirtualF () {cout<<"B"<<endl; } };intMain () {A * pa=NewA (); Pa->f (); This is obviously a b* pb= (b*) PA; Pb->f (); This forces the PA to be copied to PB, so PB points to aDeletePA,PB; Remove the address pointed to by PA,PB, but the PA, PB pointer is not removed, and the hover pointer pa=NewB (); Pa->f (); B pb= (b*) PA; Pb->f (); Breturn 0; Summary: Virtual functions are called according to the actual type of pointer, and other functions are called according to the original type.
C + + face question 1: Can a virtual function be called in constructors and fictional functions?