Question of using static_cast on "this" pointer in a derived object to base class

Posted by Johnyy on Stack Overflow See other posts from Stack Overflow or by Johnyy
Published on 2010-12-28T04:34:47Z Indexed on 2010/12/28 4:53 UTC
Read the original article Hit count: 254

Hi, 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
};

© Stack Overflow or respective owner

Related posts about c++

Related posts about inheritance