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

  Java program for presidential elections

Write a java program for Presidential Elections, the user will enter his votes based on the code for each candidate. The program will add the total votes for each candidate and proclaim the winner whoever got the most number of votes.

  Exchange the block of platinum for cash

You don't get any fractional part, as the division process rounds down the value. If you exchange the block of platinum for cash, you get m units of currency. You can do any number of exchanges for smaller blocks or currency.

  Discuss the fundamentals of object oriented programming

Include an accompanying illustration for each of the fundamentals(example, code sample, etc.)Note: Try to use Java and not C++ for examples.

  Write a method that reads the contents of the two files

BoyNames.txt This file contains a list of the 20 most popular names given to boys born in the US in 2011.

  Why is overloading useful

Overloading is a way of using the same method name for multiple methods as long as the number / types of arguments are different. Why is overloading useful? Give an example of how this could be used.

  Virtual machine forensics

Determine what you perceive to be the greatest challenge when dealing with virtual machines from a system forensics perspective. Provide a rationale with your response.

  Code that assigns true to isamember

write code that assigns true to isAMember if the value of memberID can be found in currentMembers , and that assigns false to isAMember otherwise. Use only k , currentMembers , memberID , and isAMember

  Create a class called point

Create a class called Point that has the following private members: float x - the x-coordinate of the point. float y - the y-coordinate of the point

  Create an abstract employee class

We are going to create an abstract Employee class and an abstract calculatePay method. The abstract Employee class will prevent a programmer from creating an object based on Employee

  Discuss how an interface differs from a class

Discuss how an interface differs from a class, how interfaces are defined, and what does implementing an interface mean.

  Create a sales tracking program named salestracking.java

create a sales tracking program named SalesTracking.java. This program must track monthly sales as well as compute average yearly sales, total sales for the year, and which month had the highest sales and which month had the lowest sales.

  Write an application named us insurance

Write an application named UseInsurance that uses an abstract Insurance class and Health and Life subclasses to display different types of insurance policies and the cost per month.

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