否则,新的 shared_ptr 将与r 的初始值共享所有权,但若 dynamic_pointer_cast 所进行的 dynamic_cast 返回空指针,则它为空。 令Y 为typename std::shared_ptr<T>::element_type,则将分别通过求值下列表达式,获得所得 std::shared_ptr 的存储指针: 1,2) static_cast<Y*>(r.get())...
类型转换基本上是所有的C++项目中都要用到的,在C++中主要分为四种case,分别是:static_cast、dynamic_...
cppreference-智能指针转型en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast dynamic_cast ...
static_cast转换 使用隐式和用户定义转换的组合来进行类型之间的转换。 语法 static_cast<目标类型 >(表达式 ) 返回目标类型 类型的值。 解释 只有下列转换在不移除常量性(或易变性)的场合才能用static_cast执行。 1)如果表达式 是 “cv1Base” 类型左值且目标类型 是“到cv2Deriv...
使用SFINAE 测试指针类型是否可以 static_cast 到另一个指针类型 背景 我正在写一个可移动的QScopedPointer;基本上std::unique_pointer有一些额外的访问器。我在访问 C++11 兼容编译器之前就开始了它,但现在我决心把它做好(即使我在重新发明轮子)。 问题 让我们调用我的智能指针MyUniquePointer。 我需要知道 type ...
// 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::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> ...
dynamic_cast, reinterpret_cast, static_cast and const_cast. I've tried to understand what for these are. Mostly the dynamic_cast is/was a hard topic for me. I've found a clear example here: Why can't a derived class pointer point to a base class object without casting? http://sta...
static_cast - dynamic_cast const_cast - reinterpret_cast Memory allocation new expression delete expression Classes Class declaration Constructors this pointer Access specifiers friend specifier Class-specific function properties Virtual function override specifier (C++11) final specifier (C++11) explicit ...
int* pi =reinterpret_cast<int*>(pf);简而言之,static_cast<> 将尝试转换,举例来说,如float-到-integer,而reinterpret_cast<>简单改变编译器的意图重新考虑那个对象作为另一类型。 指针类型(Pointer Types) 指针转换有点复杂,我们将在本文的剩余部分使用下面的类: ...