将共享的_ptr降低到包含feal_ptr C 11的派生类

Downcasting a shared_ptr to a derived class containing a weak_ptr c++11

本文关键字:ptr 派生 feal 共享 包含      更新时间:2023-10-16

我在尝试使用std::static_pointer_cast降低std::shared_ptr时发生了一个segfault,其中派生的类还包含 std::weak_ptr

这是MWE:

#include<iostream>                                                                                                                                                                                                  
#include<memory>                                                                                                                                                                                                    
struct Base {};                                                                                                                                                                                                     
struct Derived : Base                                                                                                                                                                                               
{                                                                                                                                                                                                                   
    std::weak_ptr<int> wp;                                                                                                                                                                                          
};                                                                                                                                                                                                                  
int main()                                                                                                                                                                                                          
{                                                                                                                                                                                                                   
    auto pB = std::make_shared<Base>();              // Create a pointer to Base class instance                                                                                                                     
    auto pD = std::static_pointer_cast<Derived>(pB); // Downcast this to a Derived class instance                                                                                                                   
    auto pint = std::make_shared<int>(0);            // Define a pointer to an integer                                                                                                                              
    std::cout << "assigning pint" << std::endl;                                                                                                                                                                     
    pD->wp = pint;                                   //Attempt to assign member of Derived                                                                                                                                                                 
    std::cout << "Did not segfault" << std::endl;                                                                                                                                                                   
    return 0;                                                                                                                                                                                                       
}

这会编译和有效,但是我不确定它是否是'好'C 11

#include<iostream>                                                                                                                                                                               
#include<memory>                                                                                                                                                                                 
struct Base {};                                                                                                                                                                                  
struct Derived : Base                                                                                                                                                                            
{                                                                                                                                                                                                
    Derived(std::shared_ptr<Base> b) : Base{*b} {}                                                                                                                                               
    std::weak_ptr<int> wp;                                                                                                                                                                       
};                                                                                                                                                                                               
int main()                                                                                                                                                                                       
{                                                                                                                                                                                                
    auto pB = std::make_shared<Base>();              // Create a pointer to Base class instance                                                                                                  
    auto pD = std::make_shared<Derived>(pB);                                                                                                                                                     
    auto pint = std::make_shared<int>(0);            // Define a pointer to an integer                                                                                                           
    std::cout << "assigning pint" << std::endl;                                                                                                                                                  
    pD->wp = pint;                                                                                                                                                                               
    std::cout << "Did not segfault" << std::endl;                                                                                                                                                
    return 0;                                                                                                                                                                                    
}