66674

Why derived class does not have the vtable pointer and used instead vtable of the base class?

Question:

I am interested in the implementation of a virtual function in pure C. Here an <a href="http://www.avabodh.com/cxxin/virtualandinheritance.html" rel="nofollow">example of the implementation</a>. Then the implementation of the derived class through a pointer to the virtual functions table of the base class. Why derived class does not have the vtable pointer and used instead vtable of the base class. Maybe because they are the same offset ?

void myClassDerived_ctor(struct myClassDerived *this) { myClassBase_ctor(&this->base); this->base.vtable = (void*)&myClassDerived_vtable + sizeof(void*); // used vtable of the base class }

Answer1:

It <em>has</em> to use the base class's vtable. The whole point is it <em>looks</em> just like a base class, but has <em>different</em> entries in the vtable. Hence it's polymorphicly <em>different</em> behaviour.

Answer2:

It <strong>does</strong> have its own vtable. It uses the base class's vtable <strong>pointer</strong> to point to it, so that code that knows only about the base class can correctly call virtual functions that are overridden in derived classes.

Answer3:

This is done to make polymorphism to work correctly. The same object can be pointed by either base class's pointer or derived class's pointer. If you call a virtual function, then in both cases it has to call the same function (the function of derived class). So the derived class makes the base class's vtable pointer to point it's own vtable so that in all cases, the object will use the correct vtable.

Recommend

  • how can I delete duplicate records from my database
  • What's the fastest way to partition a sas dataset for batch processing?
  • Can an empty virtual table exist?
  • Determine the size of object without its virtual table pointers
  • c++ reinterpret_cast, virtual, and templates ok?
  • HALF_PTR Windows data type
  • div fade-in when window is scrolled a certain distance from the top
  • How to define an array of floats in Shader properties?
  • A new chart every sheet
  • Get localized short date pattern as String?
  • In matplotlib, how do you change the fontsize of a single figure?
  • Spring: No transaction manager has been configured
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Object and struct member access and address offset calculation
  • how to find common suffix in java by using method
  • Display java JPanel in a JFrame
  • onBackPressed() not being executed
  • iOS: Detect app start via notification press
  • how to adjust image in a panel in Java swing?
  • How to access EntityManager inside Entity class in EJB3
  • how to do an event when i swipe from fragment to the other
  • JFileChooser in front of fullscreen Swing application
  • HTML download movie download link
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Submit form in a displaytag pagination
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • sending/ receiving email in Java
  • Eraser for UIBezierPath
  • Delete MySQLi record without showing the id in the URL
  • Unanticipated behavior
  • Comma separated Values
  • Suggestions to manage Login/Logout transitions
  • Trying to get generic when generic is not available
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?