An important aspect of c object and virtual function table


C++ Multiple Inheritance and Casts

An important aspect of C++ object and virtual function table (vtbl) layout is that if class D has class B as a public base class, then the initial segment of every D object must look like a B object, and similarly for the D and B virtual function tables. The reason is that this makes it possible to access any B member data or member function of a D object in exactly the same way we would access the B member data or member function of a B object. Although this works out fairly easily with only single inheritance, some effort must be put into the implementation of multiple inheritance to make access to member data and member functions uniform across publicly derived classes.

Suppose class C is de?ned by inheriting from classes A and B:

class A {

public:

int x;

virtual void f();

};

class B {

public:

int y;

virtual void f(); virtual void g();

};

class C : public A, public B  {

public:

int z;

virtual void f();

};

C *pc = new C;    B *pb = pc;    A *pa =   pc;

and pa, pb, and pc are pointers to the same object, but with different types. The representation of this object of class C and the values of the associated pointers are illustrated in this chapter.

(a) Explain the steps involved in ?nding the address of the function code in the call pc->f(). Be sure to distinguish what happens at compile time from what happens at run time. Which address is found, &A : : f(), &B : : f(), or &C : : f()?

(b) The steps used to ?nd the function address for pa->f() and to then call it are the same as for pc->f(). Brie?y explain why.

(c) Do you think the steps used to ?nd the function address for and to call pb->f() have to be the same as the other two, even though the offset is different? Why or why not?

(d) How could the call pc->g() be implemented?

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: An important aspect of c object and virtual function table
Reference No:- TGS01269967

Expected delivery within 24 Hours