1、std::static_pointer_cast():当指针是智能指针时候,向上转换,用static_cast 则转换不了,此时需要使用static_pointer_cast。 2、std::dynamic_pointer_cast():当指针是智能指针时候,向下转换,用dynamic_cast 则转换不了,此时需要使用dynamic_pointer_cast(此处注意:base基类需要至少有一个virtual成员函数(即多态类...
std::static_pointer_cast 是C++11 引入的一个模板函数,用于智能指针(如 std::shared_ptr 或std::weak_ptr)之间的类型转换。这种转换是编译时的强制转换,不会在运行时进行检查,类似于 static_cast,但专门用于智能指针,确保转换后的指针仍然保留智能指针的内存管理功能(如引用计数和自动内存释放)。
// static_pointer_cast example #include <iostream> #include <memory> struct A { static const char* static_type; const char* dynamic_type; A() { std::cout << "construct A " << std::endl; dynamic_type = static_type; } }; struct B: A { static const char* static_type; B() {...
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...
否则,新的 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())...
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所...
reinterpret_cast意图执行低级转型,实际动作及结果可能取决于编译器,这也就表示它不可移植。例如将一个pointer to int 转型为int。这一类转型在低级代码以外很少见。 旧式转型在 C++ 中仍然是合法的,但是这里更推荐使用新形式。首先,它们在代码中更加易于辨认(不仅对人,而且对 grep 这样的工具也是如此),因而得以简化...
我了解将 static_pointer_cast 与 unique_ptr 一起使用会导致所包含数据的共享所有权。 换句话说,我想做的是: {代码...} 无论如何,这样做会导致两个 unique_ptr 永远不应该同时存在,所以它只是被禁止的。 是...
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...