Function-style cast vs. constructor

后端 未结 3 1616
终归单人心
终归单人心 2021-02-20 14:38

I learned that in C++,

typedef foo* mytype;

(mytype) a        // C-style cast

and

mytype(a)         // function-style cast
         


        
3条回答
  •  旧时难觅i
    2021-02-20 15:18

    Conversion is a form of initialization. When a type is implicitly convertible to another, a functional cast is a form of direct initialization. The compiler knows which types are convertible.

    Whenever something is converted to a class type, either a converting constructor of the target type or a conversion operator of the source type is used. In your examples, both casts call the default constructor.

提交回复
热议问题