The understanding of virtual function in C + + and the table of virtual function in simple inheritance case!

Source: Internet
Author: User
Tags c constructor

Three main features of object oriented = encapsulation + inheritance + polymorphism
Encapsulation = Abstract objective things into classes, and each class controls its own data and methods.
Inheritance = Implementation Inheritance + Visual Inheritance + interface inheritance
Polymorphic = Sets the parent object to be a technique equal to one or more of its sub-objects,
After assigning a value to the parent class object with a subclass object,
The parent object can operate in a different way depending on the characteristics of the child object that is currently assigned to it

What member functions are available for C + + empty classes
1. Default constructors
2. Default copy constructor
3. Default destructor
4. Default assignment operator
5. Default accessor operator
6. Default accessor operator const
PS: The compiler does not go back to defining a member function of these classes until it is actually used.

The copy constructor and assignment operator overloads have the following two differences:
1. Copy the new class object generated by the constructor, and the assignment operator cannot;
2. Since the copy constructor constructs a new class object directly, it is not necessary to initialize the object before initializing it.
Verifies that the source object is the same as the new object. The assignment operator requires this operation, plus the assignment operation
If there is memory allocation in the original object, release the memory first.
PS: When you have a member variable of pointer type in a class, be sure to override the copy constructor and assignment operator, and do not use the default.

#include <iostream>#include <stdio.h>using namespace STD;#ifndef __gnuc__#define __ATTRIBUTE__ (x)#endif //__gnuc__ __attribute__ ((constructor))voidBefore_main () {cout<< __function__ << Endl;cout<<"This is done before the main function is run!" "<< Endl;} __attribute__ ((destructor))voidAfter_main () {cout<< __function__ << Endl;cout<<"This is done at the end of the main function!" "<< Endl;}

People who know about C + + know that virtual functions are implemented by a virtual function table (virtual table).
In this table, the master is the Address table of the virtual function of a class, which solves the problem of inheritance, overwriting, and guaranteeing the actual function of the real reaction.
Thus, in an instance of a class with virtual functions, the table is allocated to the memory of this instance, so when a subclass is manipulated with a pointer to the parent class,
This virtual function table is important, it is like a map, indicating the actual function should be called.
In the C + + standard specification, the compiler must ensure that a pointer to a virtual function table exists in the first position in the object instance, in order to ensure that the offset of the virtual function is correctly taken.
This means that we get this virtual function table from the address of the object instance, and then we can iterate over the function pointer and call the corresponding function.

//Access private virtual functions of the base classclassa{ Public:Virtual voidG () {cout<<"A:g ()"<< Endl; }Virtual voidF () {cout<<"A::f ()"<< Endl; }Virtual voidH () {cout<<"A::h ()"<< Endl; }};/ * This kind of virtual function table is arranged by vptr | | & (A::g ()) |_ _ _ _ \& (A::f ())/& (A::h ()) * /classB: Publica{ Public:Virtual voidG () {cout<<"B::g ()"<< Endl; }Virtual voidH () {cout<<"B::h ()"<< Endl; }};/ * The virtual function table for this class is Vptr | | & (A::g ())-->& (B::g ()) |_ _ _ \& (A::f ())/& (A::h ())-->& (B::h ()) * //*mark: Every class with a virtual function has a virtual function table vtable, which holds the address of the virtual function in the order of the virtual function declared in the class, the virtual function table vtable is common to all objects of this class, meaning that no matter how many class objects the user declares,   But this vtable virtual function table has only one. Inside each object with a virtual function, there is a vptr virtual function pointer, which points to the first address of vtable, and each class object has such a pointer. */
class C{public:    cout"C Constructor !" << endl; }    cout"C Destroy !" << endl; }};//会在main函数调用之前执行的函数static C s_c;
typedef void(*pfunc) (void);classaa{Private:intADoubleb Public:Virtual voidFunc () {cout<<"Aa::func ()"<< Endl; }};/ * Storage format in memory is Vptr-->a-->b | |__\ &func ()/&func () */intMain () {cout<< __function__ << Endl;cout<<"Hello world!"<< Endl;/ * Many people can not find vptr this pointer, in fact, this class B inherits from Class A, and does not have its own member variable, so it is possible to conclude that the Class B case object only vptr this pointer. */b b; PFUNC PF;//Print out the address of the virtual function table first    cout<<"virtual function table address:"<< (int*) (&AMP;B) << Endl;cout<<"virtual function table first function address:"<< (int*)(*(int*) (&AMP;B)) << Endl; for(intI=0;i<3; i++) {pf= (PFUNC) * ((int*)*(int*) (&AMP;B) +i);    PF (); }return 0;}

The understanding of virtual function in C + + and the table of virtual function in simple inheritance case!

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.