Solved

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

Posted on 2013-06-10
5
371 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 33

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 33

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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
MSVCR80.dll crash 2 141
Using Diagnostic tools in VS2015: Unresoved allocations 19 104
designing in object programming 12 94
Indy 10 not Receiving UDP broadcast 3 22
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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