Then pa and pc will contain the same value but pb will


Multiple Inheritance and Thunks

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

class A {
public:
virtual void g(); int x;
};
class B {
public:
int y;
virtual B* f(); virtual void g();
};
class C : public A, public B {
public:
int z;
virtual C* f(); virtual void g();
};
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.

Then, pa and pc will contain the same value, but pb will contain a different value; it will contain the address of the B part of the C object. The fact that pb and pc do not contain the same value means that in C++, a cast sometimes has a run-time cost. (In C this is never the case.)

Note that in our example B::f and C::f do not return the same type. Instead, C::f returns a C* whereas B::f returns a B*. That is legal because C is derived from B, and thus a C* can always be used in place of a B*.

However, there is aproblem: When the compiler sees pb->f() it does not know whether the call will return a B* or a C*. Because the caller is expecting a B*, the compiler must make sure to return a valid pointer to a B*. The solution is to have the C-as-B vtable contain a pointer to a thunk. The thunk calls C:: f, and then adjusts the return value to be a B*, before returning.

(a) Draw all of the vtables for the classes in these examples. Show to which function each vtable slot points.

(b) Does the fact that casts in C++ sometimes have a run-time cost, whereas C never does, indicate that C++ has not adhered to the principles given in class for its design? Why or why not?

(c) Because thunks are expensive and because C++ charges programmers only for features they use, there must be a feature, or combination of features, that are imposing this cost. What feature or features are these?

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: Then pa and pc will contain the same value but pb will
Reference No:- TGS01269969

Expected delivery within 24 Hours