[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

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

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
naseeam
Asked:
naseeam
  • 2
  • 2
3 Solutions
 
jkrCommented:
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
 
sarabandeCommented:
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
 
naseeamAuthor Commented:
b is a pointer to 'vehicle' class.  Then, how can b point to 'FourWheeler' class?
0
 
jkrCommented:
>>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
 
sarabandeCommented:
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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now