|
|
Foo f2;
f2.Speak();
Foo::speak
Foo *f1 = nullptr;
f1->Speak()
, that vtable gets used. f1
is pointing at either a Foo or Fooz or Foob object, and that Foo or Fooz or Foob object has a vtable, and that vtable indicates to use either Foo::speak
or Fooz::speak
or Foob::speak
. f2.Speak();
- compiler knows for sure that the function to call is Foo::speak
f1->Speak()
- compiler doesn't know what function will be called, but at runtime the object being pointed to by f1 DOES know what its own speak function is, and that gets decided and used at runtime.
Foo f2; f2 is a Foo object. Exactly a Foo object, not anything derived for a Foo, just a Foo object. The compiler can see this, right there. There, a Foo object is being created. No ambiguity, no unknowns, so when you call f2.Speak(); the function being called is Foo::speak The compiler can see all this at compile time. There is no possible alternative. |
|
|