1、std::static_pointer_cast():当指针是智能指针时候,向上转换,用static_cast 则转换不了,此时需要使用static_pointer_cast。 2、std::dynamic_pointer_cast():当指针是智能指针时候,向下转换,用dynamic_cast 则转换不了,此时需要使用dynamic_pointer_cast(此处注意:base基类需要至少有一个virtual成员函数(即多态类...
static_point_cast 是一种用于智能指针类型间的编译时强制转换的工具,它的用法与 static_cast 类似,但专门用于智能指针。它不会在运行时进行检查,适用于你明确知道对象类型并且类型转换不会失败的场景。static_point_cast 主要用于在继承层次中进行从基类智能指针到派生类智能指针的转换。
template <class T, class U> shared_ptr<T> static_pointer_cast (const shared_ptr<U>& sp) noexcept; 解释: Returns a copy of sp of the proper type with its stored pointer casted statically from U* to T*. If sp is not empty, the returned object shares ownership over sp's resources...
首先,我们需要了解static_pointer_cast的语法和语义。它的语法形式如下: ```cpp static_pointer_cast<T>(p); ``` 其中,T是目标类型,p是待转换的指针或引用。static_pointer_cast的语义是执行一次类型转换,将p转换为T类型的指针或引用。 在源码解析之前,我们需要了解C++的类型系统和编译器实现。C++的类型系统包...
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...
return static_pointer_cast<EventPoller>(getExecutor()); } };2 changes: 1 addition & 1 deletion 2 src/FMP4/FMP4MediaSource.h Original file line numberDiff line numberDiff line change @@ -108,7 +108,7 @@ class FMP4MediaSource final : public MediaSource, public toolkit::RingDelegate<F...
get() == nullptr,但对于 dynamic_pointer_cast (4),若 dynamic_cast 失败则不修改 r。 (C++20 起)参数r - 要转换的指针 注解表达式 std::shared_ptr<T>(static_cast<T*>(r.get()))、std::shared_ptr<T>(dynamic_cast<T*>(r.get())) 及std::shared_ptr<T>(const_cast<T*>(r.get())...
template <class Ty, class Other> shared_ptr<Ty> static_pointer_cast(const shared_ptr<Other>& sp); 參數Ty 傳回的共用指標控制項型別。 Other 引數會控制的型別共用指標。 Other 引數具有共用指標。備註樣板函式傳回空白 shared_ptr 物件 sp 是否為空白的 shared_ptr 物件;否則會傳回擁有資源由 sp所...
(1)static_cast 用法:static_cast < type-id > ( exdivssion ) 该运算符把exdivssion转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法: ①用于类层次结构中基类和子类之间指针或引用的转换。 a. 进行上行转换(把子类的指针或引用转换成基类表示)是安全的; ...
static_cast是可以使用的最简单的类型转换。它是编译时强制转换。它可以在类型之间进行隐式转换(例如int到float,或指针到void*),它还可以调用显式转换函数(或隐式转换函数)。 const_cast用法示例 下面是static_cast的11个使用场景示例: 1. 用于原C风格的隐式类型转换 ...