Search code examples
c++constructorstatic-cast

What happens when static_cast from derived class to base class?


While reading the C++ standard, I read that static_cast is a kind of direct initialization (C++ standard 8.5/15).
I think this means that during a static_cast, the corresponding overloaded constructor is called.
For example, there is a type B and type D derived from B, and an object D d. Then the expression static_cast<B>(d) is a static_cast expression.

As the standard means, this static_cast expression is a direct initialization. Does it mean that this will call the constructor of type B and return a new constructed object of type B?


EDIT
Another issue is how about B & b = d or B b = d? Does these two statements involve constructor of B?


Solution

  • Yes, a static_cast to an object type (i.e., not a cast to a reference or pointer type) will use the appropriate constructor to create a temporary object.