Virtual pointer Initialization

Source: Internet
Author: User
# 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.
 
 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.