What is the difference between static_cast and Implicit_cast?

后端 未结 4 612
小蘑菇
小蘑菇 2020-12-05 05:49

What is implicit_cast? when should I prefer implicit_cast rather than static_cast?

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 06:21

    Prefer implcit_cast if it is enough in your situation. implicit_cast is less powerful and safer than static_cast.

    For example, downcasting from a base pointer to a derived pointer is possible with static_cast but not with implicit_cast. The other way around is possible with both casts. Then, when casting from a base to a derived class, use implicit_cast, because it keeps you safe if you confuse both classes.

    Also keep in mind that implicit_cast is often not needed. Using no cast at all works most of the time when implicit_cast does, that's where 'implicit' comes from. implicit_cast is only needed in special circumstances in which the type of an expression must be exactly controlled, to avoid an overload, for example.

提交回复
热议问题