简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
本篇目的:理解C++之std::static_pointer_cast智能指针类型转换应用。
在C++中,std::static_pointer_cast
是一种智能指针类型转换函数,用于将一个 std::shared_ptr
智能指针转换为另一种相关类型的智能指针。它的作用是在类型转换时保持智能指针的引用计数,确保资源的正确释放。
std::static_pointer_cast
函数的用法如下所示:
std::shared_ptr<Derived> derivedPtr = std::static_pointer_cast<Derived>(basePtr);
其中Derived
是目标类型,basePtr
是指向基类类型的std::shared_ptr
智能指针。该函数将basePtr
转换为Derived
类型的智能指针derivedPtr
。
std::static_pointer_cast
的原理是使用了C++中的RTTI(Run-Time Type Information)机制,通过检查指针指向的对象的实际类型,并进行相应的类型转换操作。
std::static_pointer_cast
通常用在需要将基类类型的智能指针转换为派生类类型的智能指针的场合。它可以方便地进行基类指针到派生类指针的安全转换,同时保持智能指针的引用计数,避免资源泄漏和多次释放的问题。
#include
#include
class Base {
public:
virtual void print() {
std::cout << "Base" << std::endl;
}
};
class Derived : public Base {
public:
void print() override {
std::cout << "Derived" << std::endl;
}
};
int main() {
std::shared_ptr<Derived> derivedPtr = std::make_shared<Derived>();
std::shared_ptr<Base> basePtr = std::static_pointer_cast<Base>(derivedPtr);
basePtr->print(); // 输出: Derived
return 0;
}
#include
#include
class Base {
public:
virtual void print() {
std::cout << "Base" << std::endl;
}
};
class Derived : public Base {
public:
void print() override {
std::cout << "Derived" << std::endl;
}
};
int main() {
std::shared_ptr<Base> basePtr = std::make_shared<Derived>();
std::shared_ptr<Derived> derivedPtr = std::static_pointer_cast<Derived>(basePtr);
derivedPtr->print(); // 输出: Derived
return 0;
}
const
指针转换为非const
指针#include
#include
class Base {
public:
void print() {
std::cout << "Base" << std::endl;
}
};
int main() {
std::shared_ptr<const Base> constBasePtr = std::make_shared<const Base>();
std::shared_ptr<Base> basePtr = std::const_pointer_cast<Base>(constBasePtr);
basePtr->print(); // 输出: Base
return 0;
}
shared_ptr
转换为weak_ptr
#include
#include
class Base {
public:
void print() {
std::cout << "Base" << std::endl;
}
};
int main() {
std::shared_ptr<Base> basePtr = std::make_shared<Base>();
std::weak_ptr<Base> weakBasePtr = std::static_pointer_cast<std::weak_ptr<Base>>(basePtr.lock());
std::shared_ptr<Base> sharedBasePtr = weakBasePtr.lock();
sharedBasePtr->print(); // 输出: Base
return 0;
}
shared_ptr
转换为unique_ptr
#include
#include
class Base {
public:
void print() {
std::cout << "Base" << std::endl;
}
};
int main() {
std::shared_ptr<Base> basePtr = std::make_shared<Base>();
std::unique_ptr<Base> uniqueBasePtr = std::static_pointer_cast<std::unique_ptr<Base>>(std::move(basePtr));
uniqueBasePtr->print(); // 输出: Base
return 0;
}
shared_ptr
转换为shared_ptr
的基类#include
#include
class Base {
public:
virtual void print() {
std::cout << "Base" << std::endl;
}
};
class Derived : public Base {
public:
void print() override {
std::cout << "Derived" << std::endl;
}
};
int main() {
std::shared_ptr<Derived> derivedPtr = std::make_shared<Derived>();
std::shared_ptr<Base> basePtr = std::static_pointer_cast<Base>(derivedPtr);
basePtr->print(); // 输出: Derived
std::shared_ptr<Derived> derivedPtr2 = std::static_pointer_cast<Derived>(basePtr);
derivedPtr2->print(); // 输出: Derived
return 0;
}
#include
#include
class Base1 {
public:
virtual void print() {
std::cout << "Base1" << std::endl;
}
};
class Base2 {
public:
virtual void print() {
std::cout <<"Base2" << std::endl;
}
};
class Derived : public Base1, public Base2 {
public:
void print() override {
std::cout << "Derived" << std::endl;
}
};
int main() {
std::shared_ptr<Derived> derivedPtr = std::make_shared<Derived>();
std::shared_ptr<Base1> base1Ptr = std::static_pointer_cast<Base1>(derivedPtr);
base1Ptr->print(); // 输出: Derived
std::shared_ptr<Base2> base2Ptr = std::static_pointer_cast<Base2>(derivedPtr);
base2Ptr->print(); // 输出: Derived
return 0;
}