Reputation: 85
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.
Upvotes: 4
Views: 1677
Reputation: 1207
Easy way to think about hiding is as follows - look at the line d->f(3.14F); from the example:
Upvotes: 2
Reputation: 16148
The two function have different signatures, so f
in derived
does not override the virtual function in base
. Just because the types int
and float
can be implicitly cast does not have an effect here.
virtual void f(float) { cout << "Base::f(float)\n"; }
virtual void f(int) { cout << "Derived::f(int)\n"; }
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.
virtual void f(int) override { cout << "Derived::f(int)\n"; }
from which gcc produces the error:
virtual void Derived::f(int)’ marked override, but does not override
clang
error: 'f' marked 'override' but does not override any member functions
http://en.cppreference.com/w/cpp/language/override
EDIT:
for your second point, you can actually expose the float
overload from base
in derived
which exposes an implicitly compatible member function. like so:
class Derived : public Base {
public:
using Base::f;
virtual void f(int) { cout << "Derived::f(int)\n"; }
};
Now passing a float to member function f
binds closer to the function defined in the base and produces:
Base::f(float)
Base::f(float)
Upvotes: 10
Reputation: 14591
As the argument types of these two functions differ, the one in Derived
class does not actually override the one from the Base
. Instead Derived::f
hides Base::f
(don't have the standard with me at the moment, so I can't quote the chapter).
This means that when you call d->f(3.14f)
, the compiler doesn't even consider B::f
. It resolves the call to D::f
. However, when you call b->f(3.14f)
, the only version which compiler can choose is B::f
as D::f
doesn't override it.
Your reading of If the value can not fit into the destination type, the behavior is undefined
is wrong. It says value not type. So value 3.0f does fit into int, but 3e11 doesn't. In the latter case, the behavior is undefined. The first part of your quote, A prvalue of floating-point type can be converted to prvalue of any integer type.
explains why d->f(3.14f)
is resolved to D::f(int)
- float indeed can be converted to integer type.
Upvotes: 1