# Include <iostream> Using Namespace STD; Class Base { Public : Base () {cout <" In base "<Endl; cout <" Virtual pointer = "<( Int *) This <Endl; cout <" Address of vtable = "<( Int *)*( Int *)This <Endl; cout <" Value at vtable = "<( Int *)*( Int *)*( Int *) This <Endl; cout <Endl ;} Virtual Void F1 () {cout <" Base: F1 "<Endl ;}}; Class Drive: Public Base { Public : Drive () {cout <" In drive "<Endl; cout <" Virtual pointer = "<( Int *) This <Endl; cout <" Address of vtable = "<( Int *)*( Int *) This <Endl; cout <" Value at vtable = "<( Int *)*( Int *)*( Int *) This <Endl; cout <Endl ;} Virtual Void F1 () {cout <" Drive: F2 "<Endl ;}}; Class Mostdrive: Public Drive { Public : Mostdrive () {cout <" In mostdrive "<Endl; cout <" Virtual pointer = "<( Int *) This <Endl; cout <" Address of vtable = "<( Int *)*( Int *) This <Endl; cout <" Value at vtable = "<( Int *)*( Int *)*( Int *) This <Endl; cout <Endl ;} Virtual Void F1 () {cout <" Mostdrive: F2 "<Endl ;}}; Int Main () {mostdrive D; Return 0 ;}
Single-step tracking aboveCodeIt is found that the constructors that enter base, driver, and mostdrive in turn, each time they enter the constructor, the values of the virtual pointer are different.
It is concluded that the pointer must be initialized before entering the constructor.
If you call a virtual function in the constructor, a problem occurs.
The reason is very simple. If the virtual function is called in the drive constructor, but the virtual function pointer is not the final result, the final result needs to enter the final constructor, that is
Only the constructors of mostdrive can be determined.