Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does static_cast(*this) to a base class create a temporary copy?

Tags:

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?

like image 479
purepureluck Avatar asked Jan 31 '12 18:01

purepureluck


People also ask

What does static_cast do in C?

static_cast is used to convert from pointer to base class to pointer to derived class, or between native types, such as enum to int or float to int. The user of static_cast must make sure that the conversion is safe. The C-style cast does not perform any check, either at compile or at run time.

What is the point of static_cast?

The static_cast operator converts variable j to type float . This allows the compiler to generate a division with an answer of type float . All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.

What does static_cast char do in C++?

static_cast in C++ The static_cast is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coercion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc. This can cast related type classes.

What is static_cast void in C++?

static_cast<void>() is the 'C++ way' of writing void conversion. In the en.cppreference.com website mentioned as discards the value of the expression.


2 Answers

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(); 
like image 115
sbi Avatar answered Oct 12 '22 11:10

sbi


That's because the code is casting to a value Window instead of a reference Window&. According to the standard, this form of casting is equivalent to calling (C++11 §5.2.9/4 = C++03 §5.2.9/2)

Window __t (*this); __t.onResize(); 

which invokes the copy-constructor of Window, and performs onResize on that copy.

(The proper way of calling a method of the superclass is

Window::onResize(); 

)

like image 45
kennytm Avatar answered Oct 12 '22 13:10

kennytm