Can a pointer of a derived class be type cast to the pointer of its base class?

前端 未结 4 609
粉色の甜心
粉色の甜心 2021-01-05 09:54

The pointer of derived class returned by new can be type cast to the pointer of its base class.

Is this true or false?

I know dynamic_cast can be used to cas

4条回答
  •  孤独总比滥情好
    2021-01-05 10:23

    Casting a pointer to a derived to a pointer to base should be implicit. This is the whole point of polymorphism: An instance of a derived class should always be safely usable as an instance of the base class. Therefore, no explicit cast is necessary.

提交回复
热议问题