Question

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?

Was it helpful?

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.

OTHER TIPS

What they mean in the standard is that you can cast for example a float to an integer. At this point conversion will happen. You can say that the resulting int is directly initialized at this point.

If class that participates in casting have corresponding overloaded conversion, then yes, it will be called. I personally would not recommend to actively use this feature. Your code will be difficult to read.

No. Direct initialization means that each byte within the object of class B will be copied from object of class D, without constructors call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top