Multiple inheritance and thunks

Assignment Help JAVA Programming
Reference no: EM13963511

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?

Reference no: EM13963511

Questions Cloud

Do you have any experience with pci : Do you have any experience with PCI? Have you worked for an organization that dealt with PCI? HIPAA? SOX? What's your perspective on "compliance" versus "security" or risk reduction?
Depreciated for three years using the straight-line method : The old truck has been depreciated for three years using the straight-line method. The new truck would be recorded at
Create a proposal for vi walker silver web site : Creating a proposal for Vi Walker Silver (Links to an external site.) web site. Vi Walker Silver (Links to an external site.) is a local business that buys and sells sterling silver.
Craft a plan for identifying and monitoring risk : Project Risk Planning: Craft a plan for identifying and monitoring risk. In your plan, you could consider: The amount of uncertainty in the project and how to deal with it and The threats of greatest concern
Multiple inheritance and thunks : 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..
Reform of reimbursement systems for healthcare services : Reform of reimbursement systems for healthcare services provided to ambulatory patients began in 1992
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 ..
Consider two spin systems a and a'' placed in external field : In the most probable situation corresponding to the final thermal equilibrium, how is the energy Et of systemA related to the energy Et' of system A' ?
How elaborating evolution helps explain social change : Post an explanation of how elaborating evolution helps explain social change. Then, explain how elaborating evolutionary systems might inform how you, as a leader or manager, can enact positive social change

Reviews

Write a Review

JAVA Programming Questions & Answers

  Write a program brighterdemo that constructs a color object

In the Java library, a color is specified by its red, green, and blue components between 0 and 255. Write a program BrighterDemo that constructs a Color object with red, green, and blue values of 50, 100, and 150.

  Describe the graphical coordinate system in java

Describe the graphical coordinate system in Java. Where is the origin? What units apply to the x,y coordinates

  Write function in javascript to compute person-s gross pay

Write the function using JavaScript syntax to compute a person's gross pay for a week. The function should receive the number of hours worked and the rate of pay per hour.

  Initialized the array of words by an assignment statement.

modify the following program . The program from Lab 4 initialized the array of words by an assignment statement.

  What challenges in planning solution for programming problem

what are the three biggest challenges in planning and designing a solution for a programming problem? What can you do to overcome these challenges? How would you apply these techniques to the programs in this class?

  Coin change program

coin change program. Make change using fewest number of coins. I'm attempting dynamic programming and i may be misunderstanding the algorithm

  Project detailsyou will explore basic socket programming

project detailsyou will explore basic socket programming threading and gui design using javas network library threading

  Given a hash function hkey prepare a java simulation

given a hash function hkey prepare a java simulation program to determine each of the subsequent quantities after

  A statement telling the user if they get a discount or not

The program asks the user if they have pre-registered for a conference. If the user has pre-registered, the program should call a method named discount () that displays the message "You are pre-registered and qualify for a 10% discount.

  Create a world class that contains a 2d array

You have to create a world class that contains a 2d array then create an abstract class called organism that contains move() method the organism should move randomly one step at the time.

  Write a method, insertat that takes four parameters

Write a method, insertAt that takes four parameters

  Reads contents of two vectors

Write a program that reads contents of two vectors, and then displays the sum of these two vectors. The program should prompt the user to enter the size of the vectors first.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd