1、std::static_pointer_cast():当指针是智能指针时候,向上转换,用static_cast 则转换不了,此时需要使用static_pointer_cast。 2、std::dynamic_pointer_cast():当指针是智能指针时候,向下转换,用dynamic_cast 则转换不了,此时需要使用dynamic_pointer_cast(此处注意:base基类需要至少有一个virtual成员函数(即多态类...
1、std::static_pointer_cast():当指针是智能指针时候,向上转换,用static_cast 则转换不了,此时需要使用static_pointer_cast。2、std::dynamic_pointer_cast():当指针是智能指针时候,向下转换,用dynamic_cast 则转换不了,此时需要使用dynamic_pointer_cast。 class LidarFrame : public SensorReading; class LivoxRea...
一、static_cast和dynamic_cast区别: 1、static_cast:向上转换,例如:基类向派生类转换 2、dynamic_cast:向下转换,例如:派生类向基类转换 二、static_poonter_cast和dynamic_pointer_cast区别同上,不过这两个只适用于智能指针
std::shared_ptr<T> static_pointer_cast( std::shared_ptr<U>&& r ) noexcept; (2) (C++20 起) template< class T, class U >std::shared_ptr<T> dynamic_pointer_cast( const std::shared_ptr<U>& r ) noexcept; (3) (C++11 起) template< class T, class U >std::shared_ptr<T> ...
1,2) 若非static_cast<T*>((U*)nullptr) 良构则行为未定义。3,4) 若非dynamic_cast<T*>((U*)nullptr) 良构则行为未定义。5,6) 若非const_cast<T*>((U*)nullptr) 良构则行为未定义。7,8) 若非reinterpret_cast<T*>((U*)nullptr) 良构则行为未定义。
在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。 class B{ public: int m_iNum; virtual void foo(); }; class D:public B{ public: char *m_szName[100]; }; void func(B *pb){ D *pd1 = static_cast(pb);
在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。 class B{ public: int m_iNum; virtual void foo(); }; class D:public B{ public: char *m_szName[100]; }; void func(B *pb){ D *pd1 = static_cast(pb);
If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However,static_castrelies on the programmer’s assertion that pb points to an object of type D and simply returns a pointer to that supposed D object. ...
We use dynamic_cast and dynamic_pointer_cast to do a bunch of downcasts (base -> derived casts). I found that all dynamic_pointer_casts are unused: they are either upcasts, or casting to itself. So I changed them to static_pointer_cast for upcasts, and no-op to no-casts. We do...
inti=3;// i is not declared constconstint&rci=i;const_cast<int&>(rci)=4;// OK: modifies i dynamic_cast used for safe downcasting and runtime type checking in the context of polymorphic classes perform runtime type checking and returns a null pointer if the conversion is not possible(...