I am trying to understand the following bit of code:
#include<iostream>
using namespace std;
class Base {
public:
virtual void f(float) { cout << "Base::f(float)\n"; }
};
class Derived : public Base {
public:
virtual void f(int) { cout << "Derived::f(int)\n"; }
};
int main() {
Derived *d = new Derived();
Base *b = d;
d->f(3.14F);
b->f(3.14F);
}
This prints
Derived::f(int)
Base::f(float)
And I am not sure why exactly.
The first call d->f(3.14F) calls the function f in Derived. I'm not 100% sure why. I had a look at this (http://en.cppreference.com/w/cpp/language/implicit_cast) which says:
A prvalue of floating-point type can be converted to prvalue of any integer type. The fractional part is truncated, that is, the fractional part is discarded. If the value can not fit into the destination type, the behavior is undefined
Which to me says you can't do this since a float does not fit into an int. Why is this implicit conversion allowed?
Secondly, even if I just accept the above as being OK, the 2nd call to b->f(3.14F) doesnt make sense. b->f(3.14F) is calling a virtual function f, so this is dynamically resolved to call the f() associated with the dynamic type of the object pointed to by b, which is a Derived object. Since we are allowed to convert 3.14F into an int, because the first function call indicates that this is legal, this (to my understanding) should call the Derived::f(int) function again. Yet it calls the function in the Base class. So why is this?
edit: here's how I figured it out and explained it to myself.
b is a pointer to a Base object, therefore we can only use b to access members of a Base object, even if b really points to some Derived object (this is standard OO/inheritance stuff).
The only exception to this rule is when a member function of Base is declared as virtual. In such a case, a Derived object may override this function, providing another implementation by using the exact same signature. If this occurs, then this Derived implementation will be called at run time even if we happen to be accessing the member function through the pointer to a Base object.
Now, in the snippet of code above, we do not have any overriding taking place because the signatures of B::f and D::f are different (one is a float, the other an int). So when we call b->f(3.14F), the only function that is considered is the original B::f, which is what is called.
The two function have different signatures, so
f
inderived
does not override the virtual function inbase
. Just because the typesint
andfloat
can be implicitly cast does not have an effect here.A clue to what is happening can been seen with the new
override
keyword in C++11, this is very effective at reducing these sort of bugs.from which gcc produces the error:
clang
http://en.cppreference.com/w/cpp/language/override
EDIT:
for your second point, you can actually expose the
float
overload frombase
inderived
which exposes an implicitly compatible member function. like so:Now passing a float to member function
f
binds closer to the function defined in the base and produces: