Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let me consolidate the previous answers.</p> <p>There are actually two issues here. One is slicing. You are initializing an array of virs with a copy of a subvir. In such cases the compiler slices the vir part out of the subvir and copies it into the array, so you really do get only vir objects there. Now in your particular case, subvir has no additional data members beyond those of vir, so slicing is somewhat degenerate and the vir object looks a lot like a subvir one. However, vir and subvir are different classes and the object in the array ends up being a vir object and not a subvir object disguised as vir. One way the difference between the two would manifest practically, even if both had the same data members, is if vir had virtual functions overloaded by subvir. In that case the vtable pointer in the object in the array would point to vir's vtable, not subvir's. Of course, it would be even more explicit if subvir were to contain additional data members not found in vir.</p> <p>The second issue is polymorphism. At the point of usage (the call to move()) the compiler thinks you are calling the move() method of an object of type vir (since the array is an array of virs). (The compiler is of course correct in thinking so due to the slicing, degenerate as it may be in this case.) Had it actually been a subvir object as you intented, you could get subvir::move() called by making move() virtual in vir.</p> <p>To get the desired behavior you could use an array of pointers (but then you would be operating directly on sv1, not a copy of it, unless you first created a copy and initialized the array with a pointer to the copy).</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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