?
Solved

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

Posted on 2013-06-10
5
Medium Priority
?
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1000 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 35

Assisted Solution

by:sarabande
sarabande earned 600 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
 
LVL 1

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 1000 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 35

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

752 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