Downcasting shared pointer to derived class with additional functionality - is this safe?

前端 未结 3 733
时光取名叫无心
时光取名叫无心 2021-02-05 19:23

Consider the following outline:

class Base { /* ... */ };

class Derived : public Base
{
public:
    void AdditionalFunctionality(int i){ /* ... */ }
};

typedef         


        
3条回答
  •  长情又很酷
    2021-02-05 20:07

    Does the Base has a virtual destructor? If yes then it is safe to use downcasting. In your incorrect sample pDerived should be NULL in result, so you need to check the result of dynamic_pointer_cast every time.

提交回复
热议问题