3

Suppose we have a base class Aand derived class B. A implements a virtual method foo() which is overloaded in B.

If we have an object test of type B, then the object would surely contain both implementations of foo(). When we call the method, how is the correct (most derived) method found?

Would the vptr contained in the base class come into use perhaps...?

M-R
  • 237

1 Answers1

4

If a function/method is virtual, that means the object has a table of pointers, and one of them points to that function. (As @gnasher729 pointed out, that table is shared among all instances of a class, so it takes minimal space.)

If B does not override the function, then the table points to A's version of the function.

If A defines the function as pure virtual, then B has to override it, because A has no definition for the function.

Mike Dunlavey
  • 12,905