-1
当试图使用std::static_pointer_cast
向下转换std::shared_ptr
时,发生段错误,其中派生类还包含std::weak_ptr
。将shared_ptr向下转换为包含weak_ptr C++的派生类11
这里是一个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;
}
你'static_pointer_cast'是未定义行为当然,因为'pB'实际上并不指向'pD'类型的对象。 –
我明白了。我清楚地误解了'std :: static_pointer_cast'的作用。 你会如何建议我实现我想要的,即将底层的'Base'升级为'Derived',最后产生一个'std :: shared_ptr'? –
问题是你实际上在构造'Base',所以当你将它转换到Derived'(它工作)时,你在访问它时会得到未定义的行为,因为从来没有真正分配过Derived的内存。在这种情况下,你会遇到段错误。 – Donnie