this is an example taken from Effective C++ 3ed, it says that if the static_cast is used this way, the base part of the object is copied, and the call is invoked from that part. I wanted to understand what is happening under the hood, will anyone help?
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
};
This:
is effectively the same as this:
The first line creates a copy of the
Windowbase class subobject of theSpecialWindowobject pointed to bythis. The second line callsonResize()on that copy.This is important: you never call
Window::onResize()on the object pointed to bythis; you callWindow::onResize()on the copy ofthisthat you created. The object pointed to bythisis not touched after you make the copy it.If you want to call
Window::onResize()on the object pointed to bythis, you can do so like this: