Solved

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

Posted on 2013-06-10
5
374 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 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 34

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
 
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 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 34

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel/Word Add-in in what language? 5 142
Precision Problem in C++ 7 48
How do i run a c++ file? 15 51
find Speed using GPS latitude & longitude 4 83
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 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