若r为空,则新的shared_ptr亦然(但其存储指针不必为空)。否则,新的shared_ptr将与r的初始值共享所有权,但若dynamic_pointer_cast所进行的dynamic_cast返回空指针,则它为空。 令Y为typenamestd::shared_ptr<T>::element_type,则将分别通过求值下列表达式,获得所得std::shared_ptr的存储指针: ...
cppreference-智能指针转型en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast dynamic_cast ...
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> ...
// std_tr1__memory__static_pointer_cast.cpp // compile with: /EHsc #include <memory> #include <iostream> struct base { int val; }; struct derived : public base { }; int main() { std::shared_ptr sp0(new derived); std::shared_ptr<derived> sp1 = std::static_pointer_cast<derived...
The expressionsstd::shared_ptr<T>(static_cast<T*>(r.get())),std::shared_ptr<T>(dynamic_cast<T*>(r.get()))andstd::shared_ptr<T>(const_cast<T*>(r.get()))might seem to have the same effect, but they all will likely result in undefined behavior, attempting to delete the same...
cppreference.com Log in NamespacesPage DiscussionVariantsViews View Edit History Actionsstd::experimental::static_pointer_cast, std::experimental::dynamic_pointer_cast, std::experimental::const_pointer_cast, std::experimental::reinterpret_pointer_cast...
// std_tr1__memory__static_pointer_cast.cpp // compile with: /EHsc #include <memory> #include <iostream> struct base { int val; }; struct derived : public base { }; int main() { std::shared_ptr sp0(new derived); std::shared_ptr<derived> sp1 = std::static_pointer_cast<derived...
// std_tr1__memory__static_pointer_cast.cpp // compile with: /EHsc #include <memory> #include <iostream> struct base { int val; }; struct derived : public base { }; int main() { std::shared_ptr sp0(new derived); std::shared_ptr<derived> sp1 = std::static_pointer_cast<derived...
// std_tr1__memory__static_pointer_cast.cpp // compile with: /EHsc #include <memory> #include <iostream> struct base { int val; }; struct derived : public base { }; int main() { std::shared_ptr sp0(new derived); std::shared_ptr<derived> sp1 = std::static_pointer_cast<derived...
// std_tr1__memory__static_pointer_cast.cpp // compile with: /EHsc #include <memory> #include <iostream> struct base { int val; }; struct derived : public base { }; int main() { std::tr1::shared_ptr sp0(new derived); std::tr1::shared_ptr<derived> sp1 = std::tr1::static_...