C++之智能指针类型转换应用总结(二百二十九)

简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!

优质专栏:Audio工程师进阶系列原创干货持续更新中……

人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.

更多原创,欢迎关注:Android系统攻城狮

欢迎关注Android系统攻城狮

1.前言

本篇目的:理解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通常用在需要将基类类型的智能指针转换为派生类类型的智能指针的场合。它可以方便地进行基类指针到派生类指针的安全转换,同时保持智能指针的引用计数,避免资源泄漏和多次释放的问题。

2.应用实例

v1.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<Derived> derivedPtr = std::make_shared<Derived>();
    std::shared_ptr<Base> basePtr = std::static_pointer_cast<Base>(derivedPtr);

    basePtr->print(); // 输出: Derived

    return 0;
}
  • 注意使用std::static_pointer_cast将派生类Derived转换成基类Base。

v2.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;
}
  • 注意使用std::static_pointer_cast将基类Base转换成派生类Derived类型。

v3.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;
}
  • 注意使用std::static_pointer_cast将基类Base转换成基类Derived类型

v4.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;
}
  • 注意使用std::static_pointer_cast将基类shared_ptr转换成基类weak_ptr类型

v5.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;
}
  • 注意使用std::static_pointer_cast将基类shared_ptr转换成基类unique_ptr类型

v6.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;
}
  • 注意使用std::static_pointer_cast将shared_ptr转换类shared_ptr基类。

v7.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;
}
  • 注意使用std::static_pointer_cast将派生类Derived转换基类Base1,再转换成Base2类型。

你可能感兴趣的:(C++入门系列,c++,智能指针类型转换)