我有一个类层次结构,其中B
来自A
,如下所示:
class A : public std::enable_shared_from_this<A>
{
};
class B : public A
{
void f()
{
// the code below compiles
std::shared_ptr<B> copyOfThis = std::static_pointer_cast<B>(shared_from_this());
// the code below does not
std::shared_ptr<B> copyOfThis = static_cast<std::shared_ptr<B>>(std::make_shared<A>(shared_from_this()));
}
};
所以实际上我想理解为什么我不能使用static_cast
将shared_ptr
父类强制转换为子类,当它实际包含子类this
时。
编辑:看看这个问题:Polymorphic smart pointer usage这里我问为什么可以在父级的共享指针上输入子的共享指针。答案是有一个模板构造函数。查看问题中的详细信息。那么,即使shared_ptr<A>
和shared_ptr<B>
之间没有任何关系,这个构造函数也无法帮助进行投射。
答案 0 :(得分:9)
您无法将shared_ptr<A>
强制转换为shared_ptr<B>
,因为类型之间没有继承关系。出于同样的原因,您无法将vector<A>
转换为vector<B>
。
使用相关类型实例化类模板不会使模板实例化也相关。
shared_ptr<T>(const shared_ptr<Y>&)
构造函数没有帮助,因为它仅在Y*
可隐式转换为T*
时才可用,即它支持与隐式发生的指针转换相同的指针转换,例如B*
至A*
,不 A*
至B*
。
可以做的是:
shared_ptr<A> thisA = shared_from_this();
shared_ptr<B> thisB(thisA, static_cast<B*>(thisA.get()));
这会创建shared_ptr<B>
与thisA
共享所有权,并保留指针static_cast<B*>(thisA.get())
这正是static_pointer_cast<B>(thisA)
的作用,但上面使用的别名构造函数稍后被添加到shared_ptr
,因此在static_pointer_cast
被发明时不存在,而且它也少得多清楚它在做什么。 static_pointer_cast
更具表现力。如果要对指针类型执行静态强制转换,请使用static_pointer_cast
。
答案 1 :(得分:5)
长话短说:std::shared_ptr<A>
不是std::shared_ptr<B>
的父级。这就是static_pointer_cast
存在的全部要点。