What exactly is or was the purpose of C++ function-style casts?

后端 未结 6 1928
Happy的楠姐
Happy的楠姐 2020-11-30 07:17

I am talking about \"type(value)\"-style casts. The books I have read pass over them quickly, saying only that they are semantically equivalent to C-style casts, \"(type) va

6条回答
  •  生来不讨喜
    2020-11-30 08:01

    C-style casts should not be used.

    Function-style casts should be used, especially when the target type is a class name (or class template specialization). They fit the pattern of apparent constructor calls in the case with one argument.

    MyClass( expr );   // Creates temporary, initialized like MyClass obj( expr );
    MyClass( e1, e2 ); // Similar, and no other way to write this.
    int( expr );       // Function-style cast.
    

提交回复
热议问题