C + + inheritance, face question at virtual function

Source: Internet
Author: User

Yesterday, received Sensetime company Interviewer's telephone interview (three companies a day, a lot of harvest), talk time of nearly 1.5 hours, the interview process exposed a lot of knowledge loopholes, this article for the interview process of inheritance and virtual function aspects of knowledge to do a summary, check the gaps, hope to help everyone.

virtual function table under single inheritance

Single inheritance under virtual function table: How to organize the class a{public:virtual void Func () {cout << "a::func" << Endl;} virtual void Funca () {cout << "A::funca" << Endl;}}; Class b:public a{public:virtual void Func () {cout << "b::func" << Endl;} virtual void Funcb () {cout << "B::FUNCB" << Endl;}}; Class c:public a{public:virtual void Func () {cout << "c::func" << Endl;} virtual void FUNCC () {cout << "C::FUNCC" << Endl;}}; typedef void (*FUNC) (); int main () {a A; b b; C c;cout << "A:: Virtual table:" << Endl ((func) (* (int *) (* (int*) (&a)))) ();((FUNC) (* ((int*) (* (int*) (&a) + 1)) ) (); cout << "-------------------------------------" << endl;cout << "B:: Virtual table:" << Endl; ((FUNC) (* (int *) (* (int*) (&b)))) ();((func) (* ((int*) (* (int*) (&b) + 1))) ();((func) (* (int*) (* (int*) (&b) + 2))) (); cout << "------------ -------------------------"<< endl;cout <<" C:: Virtual table: "<< Endl; ((FUNC) (* (int *) (* (int*) (&c)));( (FUNC) (* (int*) (*(int*) (&c)) + 1))) ();((FUNC) (* ((int*) (* (int*) (&c) + 2))) (); System ("pause"); return 0;}


Question 1: There are virtual functions in all three classes, so three classes have their own separate virtual function tables

Issue 2: The organization of three virtual function tables, as shown in

Problem 3:b, c each have their own virtual function table, do not affect each other

Issue 4: The object of the class only stores pointers to virtual function tables Vfptr (typically stored in the front of the object memory layout), only one copy of the virtual function table, shared for all objects,vtable stored in the Linux/unix read-only data segment of the executable file ( Rodata), while Microsoft's compiler stores the virtual function table in a constant segment, click the Open link

typedef void (*func) (), Class a{public:virtual void FUNC () {cout << "a::func" << Endl;} virtual void Funca () {cout << "A::funca" << Endl;} Private:int A;}; Class b{public:virtual void Func () {cout << "b::func" << Endl;} virtual void Funcb () {cout << "B::FUNCB" << Endl;} Private:int b;}; Class C:p ublic A, public b{public:virtual void func () {cout << "c::func" << Endl;} virtual void FUNCC () {cout << "C::FUNCC" << Endl;} Private:int c;}; typedef void (*func) (), Class a{public:virtual void FUNC () {cout << "a::func" << Endl;} virtual void Funca () {cout << "A::funca" << Endl;} Private:int A;}; Class b{public:virtual void Func () {cout << "b::func" << Endl;} virtual void Funcb () {cout << "B::FUNCB" << Endl;} Private:int b;}; Class C:p ublic A, public b{public:virtual void func () {cout << "c::func" << Endl;} virtual void FUNCC () {cout << "C::FUNCC" << Endl;} Private:int c;};


Virtual function table under multiple inheritance conditions

Virtual function table under multiple inheritance conditions void Test () {C c;cout << "virtual function table under multiple inheritance conditions:" << endl;cout << "------------------------" < < Endl; ((FUNC) (* ((int*) (* (* () (*) (&C)))) ();((func) (* (int*) (* (int*) (&c)) + 1))) ();((FUNC) (* ((int*) (* ( int*) (&c)) + 2)) () cout << "------------------------" << Endl ((FUNC) (* (int*) (* (int*) (&c) + 2)) ) ();((FUNC) (* ((int*) (* ((int*) (&c) + 2) + 1))) ();



The memory space distribution for object C is as follows:


A function that a base-class pointer can access when a base-class pointer points to a derived class under multiple inheritance conditions

Under multiple inheritance conditions, when a base class pointer is directed to a derived class, the base class pointer can access the function void Test1 () {c C; A *pa = &c; B *PB = &c; C *pc = &c;cout << "The function that the base class pointer pa can invoke:" << endl;pa->func ();p A->funca ();//PA->FUNCB (); ERROR: Hint Class A does not have a member FUNCB, FUNCC-  to-type restriction//PA->FUNCC (); errorcout << "Base-class pointer PB can invoke functions:" << endl;pb- >func ();p B->FUNCB ();//pb->funca (); ERROR//PB->FUNCC (); errorcout << "derived class pointer a function that a PC can invoke:" << Endl;pc->func ();p C->funca ();p C->FUNCB ();p C->FUNCC ();


Problem 1:pa and PC are the same value, are the first address of object C, Pb and Pa so far four bytes (caused by int A and observed memory space allocation)

Issue 2: A base-class pointer to an object of a derived class that can be accessed by a function of the base-class pointer is limited by the type (which function the runtime invokes is affected by polymorphism)

3: Due to polymorphism, the Func of Class C is accessed

4: By adding scope: Pa->a::func ()


Under multiple inheritance conditions, after a base class pointer points to a derived class object, the function that can be accessed after the type conversion is coerced between the base class pointers

void Test2 () {c C; A *pa = &c; B *PB = &c; C *pc = &c;pa = Reinterpret_cast<a *> (pb);p a->func ();p A->funca ();      PB = Reinterpret_cast<b *> (PA);//pb->func ();//PB->FUNCB ();//pa = Reinterpret_cast<a *> (PC);//pa- >func ();//pa->funca ();}


The results are very strange (see Assembly analysis)



The last simple question is as follows


I believe you after the analysis of the above problems, will certainly answer (the answer is already in the picture ^_^)

C + + inheritance, face question at virtual function

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.