Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're pointing to an object of type <code>NuclearSubmarine</code> with a pointer to type <code>Car</code>. You can't do that, because these types are unrelated.</p> <p>This error invokes undefined behaviour, which causes your program to behave in an unpredictable way.</p> <hr> <p>If you're interested in why this happens, have a read on how virtual functions are implemented internally: <a href="http://en.wikipedia.org/wiki/Virtual_method_table" rel="nofollow">Virtual method table</a>. This will make things clear for you.</p> <hr> <p>In reply to the comment:</p> <p>That's right. <code>Vehicle</code> instances all have an internal pointer that points to a vtable which looks something like this:</p> <pre><code>0: Vehicle::~Vehicle 1: Vehicle::StartEngine // this is a null pointer </code></pre> <p><code>Car</code> instances have their vptr point to a vtable which looks something like this:</p> <pre><code>0:Vehicle::~Vehicle // this one didn't get overridden 1:Car::startEngine 2:Car::openGasTrap </code></pre> <p><code>NuclearSubmarine</code>'s vtable looks like this:</p> <pre><code>0:Vehicle::~Vehicle // this one didn't get overridden 1:NuclearSubmarine::startEngine 2:NuclearEngine::fireNuclearMissile. </code></pre> <p>If you have this,</p> <pre><code>Vehicle* v = new Car(); v-&gt;startEngine(); </code></pre> <p>it gets compiled into something like this (pseudocode ahead):</p> <pre><code>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); </code></pre> <p>The virtual pointer refers the function lookup into the correct vtable w/r/t the object's actual runtime type.</p> <p>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.</p> <p>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.</p> <p><em>(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.)</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload