它们的功能和std::static_cast()、std::dynamic_cast、std::const_cast()和std::reinterpret_cast()类似,只不过转换的是智能指针std::shared_ptr,返回的也是std::shared_ptr类型。 1、std::static_pointer_cast():当指针是智能指针时候,向上转换,用static_cast 则转换不了,此时需要使用static_pointer_cast。
七十八、static_cast和dynamic_cast,static_poonter_cast和dynamic_pointer_cast区别,一、static_cast和dynamic_cast区别:1、static_cast:向上转换,例如:基类向派生类转换2、dynamic_cast:向下转换,例如
static_pointer_cast template<classT,classU>std::shared_ptr<T>static_pointer_cast(conststd::shared_ptr<U>&r)noexcept{autop=static_cast<typenamestd::shared_ptr<T>::element_type*>(r.get());returnstd::shared_ptr<T>{r, p};} dynamic_pointer_cast ...
const_pointer_cast Function declare_no_pointers declare_reachable default_delete dynamic_pointer_cast Function get_deleter Function get_pointer_safety get_temporary_buffer make_shared (<memory>) owner_less pointer_safety return_temporary_buffer static_pointer_cast Function swap Function <memory> unchecked...
static_pointer_cast template<classT,classU>std::shared_ptr<T>static_pointer_cast(conststd::shared_ptr<U>&r)noexcept{autop=static_cast<typenamestd::shared_ptr<T>::element_type*>(r.get());returnstd::shared_ptr<T>{r, p};} dynamic_pointer_cast ...
附言:我知道它适用于static_pointer_cast,但想知道是什么导致了dynamic_pointer_cast的问题! struct Base { Base() = default; virtual ~Base() = default; //Copy Base(const Base&) = default; Base& operator=(const Base&) = default; //Move ...
();fails with access violation, because it calls function on a null pointer. Usingstatic_castinstead permits, incorrectly, upgrading the interface toderived. In this case,d_ptr->show();calls the functionvoidderived::show()on object of dynamic typebase.derived::showtries to accessderived::i...
std::shared_ptr<T> static_pointer_cast( const std::shared_ptr<U>& r ) noexcept; (1) (C++11 起) template< class T, class U >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> ...
template< class T, class U > std::shared_ptr<T> static_pointer_cast( const std::shared_ptr<U>& r ) noexcept { auto p = static_cast<typename std::shared_ptr<T>::element_type*>(r.get()); return std::shared_ptr<T>(r, p); } 版本二 template< class T, class U > std::sh...
然后我尝试使用dynamic_pointer_cast来键入B,但boost :: dynamic_pointer_cast返回NULL。 当我执行boost :: static_pointer_cast并使用指针时,它不会使程序崩溃。但我更愿意,如果动态演员工作。 为什么会这样? 编辑:这是代码的一个紧凑部分(否则它放在这里太长了)。如果您需要的不仅仅是头文件,请告诉我。