Solved

How to determine whether base or derived class member function is called?

Posted on 2013-06-10
5
361 Views
Last Modified: 2013-06-11
Please open below link on virtual functions:

http://www.exforsys.com/tutorials/c-plus-plus/c-virtual-functions.html

Please clarify last Three paragraphs.  In the last example, first base class virtual function is called?  Why? Is it because base class was instantiated?
Then, derived class function is called, why?


Thank you!
0
Comment
Question by:naseeam
  • 2
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 39236555
Iit is called because it is actually the base class that was instantiated in that example. The second call instantiates a derived clas object, which in turn is being called in the 2nd step. The pointer types are identical, but the implementations are different, which is what makes the C++ virtuality so powerful.
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 150 total points
ID: 39236896
in general, you could make the decision by asking the "Is A" question. if the object "is a" FourWheeler the virtual override of FourWheeler::Make was called. if the function is not defined (or hasn't the right arguments), the Vehicle::Make was called cause each FourWheeler also "is a" Vehicle. the latter also would apply if it "is a" Vehicle only.

normally it makes not so much sense to instantiate baseclass objects cause those kind of objects are artificial and abstract. you can forbide the instatiation of baseclass objects by making (at least one of) the virtual function(s) pure virtual in the baseclass.

Sara
0
 

Author Comment

by:naseeam
ID: 39237607
b is a pointer to 'vehicle' class.  Then, how can b point to 'FourWheeler' class?
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 39237921
>>Then, how can b point to 'FourWheeler' class?

This is what I meant with "[...] is what makes the C++ virtuality so powerful.". 'FourWheeler' is derived from 'Vehicle' and they share the same virtual interface. So, these virtual methods case the compiler to add a VTBL (virtual method table) - see http://en.wikipedia.org/wiki/VTBL
That article explains it much better than I ever could, so in short:

An object's dispatch table will contain the addresses of the object's dynamically bound methods. Method calls are performed by fetching the method's address from the object's dispatch table. The dispatch table is the same for all objects belonging to the same class, and is therefore typically shared between them. Objects belonging to type-compatible classes (for example siblings in an inheritance hierarchy) will have dispatch tables with the same layout: the address of a given method will appear at the same offset for all type-compatible classes. Thus, fetching the method's address from a given dispatch table offset will get the method corresponding to the object's actual class

Through that means, it does not matter if 'b' is actually a 'FourWheeler' or a 'Vehicle' (with the 1st being a subset to the latter), at runtime, the correct implementation will be called.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 39238388
b is a pointer to 'vehicle' class.  Then, how can b point to 'FourWheeler' class?

the FourWheeler class is derived from Vehicle class. that means each pointer to a FourWheeler class is also a pointer to the Vehicle part of the FourWheeler object. if using single inheritance the pointer to baseclass part and the pointer to the derived object have the same value. the structure in memory looks like the following:

[[data members of Vehicle][additional data members of FourWheeler]]

Open in new window


b is a pointer variable which has a memory address that points to the object structure of the class that was created. if you created a FourWheeler it would point to FourWheeler structure. if you created a Vehicle (only) b points only to a shorter structure. that is why you can have a Vehicle pointer b that points to a FourWheeler object.

note, a pointer variable is an own data type. it is not identical with the object it points to. you may have two or more pointer variables pointing to the same object. pointers can be NULL or - when pointing to an array element of a pointer array - they can be incremented, then pointing to the next array element. as explained by jkr, baseclass pointers have one more speciality, the Virtual Method Table (VMT) they were associated to. the compiler knows of (possible) virtual functions at compile time. because of that it could add an association to the VMT to each of the pointer variables. therefore it can decide at runtime which class the real object is and choose the appropriate virtual function from that.

Sara
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now