Search code examples
c++inheritancevirtual

Why it is giving derived version?


I was trying below code:

In which I am calling opengascap() of car class with car* pointer which is pointing to a object of nuclear** class but it is giving nuclear*** function of the object that is being pointed to. My question is why its giving output "fire" although the function name doesn't even exist in the nuclearsubmarine class.

  #include <iostream>

  using namespace std;

  class Vehicle 
  {
      public:
      virtual ~Vehicle() { }
      virtual void startEngine() = 0;
  };

  class Car : public Vehicle 
  {
  public:
      virtual void startEngine()
      {
          cout<<"start car";
      }
      virtual void openGasCap()
      {
      cout<<"open";
      }
  };

  class NuclearSubmarine : public Vehicle 
  {
      public:
      virtual void startEngine()
      {
          cout<<"start ship";
      }
      virtual void fireNuclearMissle()
      {
          cout<<"fire";
      }
  };

 int main()
 {
     Car   car;
     Car*  carPtr = &car;
     NuclearSubmarine  sub;
     NuclearSubmarine* subPtr = &sub;
     carPtr=(Car*)subPtr;
     // This last line would have caused carPtr to point to sub !
     carPtr->openGasCap();  // This might call fireNuclearMissle()!
     return 0;
 }

Ouput: fire


Solution

  • You're pointing to an object of type NuclearSubmarine with a pointer to type Car. You can't do that, because these types are unrelated.

    This error invokes undefined behaviour, which causes your program to behave in an unpredictable way.


    If you're interested in why this happens, have a read on how virtual functions are implemented internally: Virtual method table. This will make things clear for you.


    In reply to the comment:

    That's right. Vehicle instances all have an internal pointer that points to a vtable which looks something like this:

    0: Vehicle::~Vehicle
    1: Vehicle::StartEngine                   // this is a null pointer
    

    Car instances have their vptr point to a vtable which looks something like this:

    0:Vehicle::~Vehicle                       // this one didn't get overridden
    1:Car::startEngine
    2:Car::openGasTrap
    

    NuclearSubmarine's vtable looks like this:

    0:Vehicle::~Vehicle                       // this one didn't get overridden
    1:NuclearSubmarine::startEngine
    2:NuclearEngine::fireNuclearMissile.
    

    If you have this,

    Vehicle* v = new Car();
    v->startEngine();
    

    it gets compiled into something like this (pseudocode ahead):

    Vehicle* v = new Car();
    // OK, v is a Vehicle and startEngine is a Vehicle's virtual function of index 1
    // let's call it!
    StartEngineFunc* f = v.VPTR[1]; // look up the vtable using the object's virtual pointer
    CallMethod(v, f);
    

    The virtual pointer refers the function lookup into the correct vtable w/r/t the object's actual runtime type.

    This allows you to call methods of derived classes via pointers to base classes, since derived classes will have their virtual table (the first part of it) corresponding to its parent class.

    However, if two classes are not related by a parent-child relationship, their virtual tables will have different meaning. That's what happens in your case.

    (Note that while the vtable mechanism is an implementation detail - it's very common in compilers, but it isn't enforced in any way and compilers are free to implement it differently - so you shouldn't count on that in your programs.)