I'm reading Effective C++ and came across this example:
class Window { // base class
public:
virtual void onResize() { ... } // base onResize impl
...
};
class SpecialWindow: public Window { // derived class
public:
virtual void onResize() { // derived onResize impl;
static_cast<Window>(*this).onResize(); // cast *this to Window,
// then call its onResize;
// this doesn't work!
... // do SpecialWindow-
} // specific stuff
...
};
The book says:
What you might not expect is that it does not invoke that function on the current object! Instead, the cast creates a new, temporary copy of the base class part of *this, then invokes onResize on the copy!
Why does static_cast (above code) create a new copy? Why not just just use the base class part of the object?
Because this code asks to create a new object. This code wants to make a Window
object from *this
— which can be done using the copy constructor of Window
.
What you want instead is this:
static_cast<Window&>(*this).onResize();
// ^
// note the &
This means I want to make a Window&
from *this
— which is an implicit conversion from a derived class' reference (*this
is a SpecialWindow&
) to a Window&
reference.
However, it's better to just call the specific version of the member function onResize()
you want to call:
Window::onResize(); // equivalent to this->Window::onResize();