如何使用智能指针动态向上转换和向下转换?
How do I dynamic upcast and downcast with smart pointers?
我有一些带有通用接口的代码,我需要在其中上下转换。我现在正在尝试转换为智能指针,但是 运行 出现了一些错误。下面的代码重现了这个问题。我正在使用 C++14,所以我认为这些东西现在应该可以自动运行了?
#include <memory>
#include <iostream>
int main()
{
std::shared_ptr<int> a(new int);
*a = 5;
std::shared_ptr<void> b = std::dynamic_pointer_cast<void>(a);
std::shared_ptr<int> c = std::dynamic_pointer_cast<int>(b);
std::cout << *c; //should be 5
return 0;
}
然而,当我尝试编译它时,我得到:
Error C2680 '_Elem1 *': invalid target type for dynamic_cast
Error C2681 'int *': invalid expression type for dynamic_cast
猜猜我在语法上做了什么蠢事?
从 C++17 开始,您有 std::reinterpret_pointer_cast
规则与哑指针相同,除了您必须使用 std::static_pointer_cast
和 std::dynamic_pointer_cast
而不是 static_cast
和 dynamic_cast
。 C++17 将引入 reinterpret_cast
的对应项,但这对于向上或向下转换以及从 void 或到 void 的转换都不需要。
How do I dynamic upcast ... with smart pointers?
类型层次结构中的向上转换是隐式的,因此不需要强制转换:
struct A{
virtual ~A(){}
};
struct B:A{};
auto b = std::make_shared<B>();
std::shared_ptr<A> a = b;
How do I dynamic ... downcast with smart pointers?
如果您不确定源是否指向正确的类型,请使用 std::dynamic_pointer_cast
。这只有在继承是多态的(即基类至少有一个虚函数)时才有可能:
b = std::dynamic_pointer_cast<B>(a);
或std::static_pointer_cast
如果你知道类型是正确的:
b = std::static_pointer_cast<B>(a);
std::shared_ptr<void> b = std::dynamic_pointer_cast<void>(a);
std::shared_ptr<int> c = std::dynamic_pointer_cast<int>(b);
void
和int
没有继承关系,不能向上转型或向下转型。
但是,所有指针都可以隐式转换为 void 指针,因此可以进行隐式转换:
std::shared_ptr<void> b = a;
并且,void 指针可以静态转换为原始类型,因此您可以使用 std::static_pointer_cast
:
std::shared_ptr<int> c = std::static_pointer_cast<int>(b);
我有一些带有通用接口的代码,我需要在其中上下转换。我现在正在尝试转换为智能指针,但是 运行 出现了一些错误。下面的代码重现了这个问题。我正在使用 C++14,所以我认为这些东西现在应该可以自动运行了?
#include <memory>
#include <iostream>
int main()
{
std::shared_ptr<int> a(new int);
*a = 5;
std::shared_ptr<void> b = std::dynamic_pointer_cast<void>(a);
std::shared_ptr<int> c = std::dynamic_pointer_cast<int>(b);
std::cout << *c; //should be 5
return 0;
}
然而,当我尝试编译它时,我得到:
Error C2680 '_Elem1 *': invalid target type for dynamic_cast
Error C2681 'int *': invalid expression type for dynamic_cast
猜猜我在语法上做了什么蠢事?
从 C++17 开始,您有 std::reinterpret_pointer_cast
规则与哑指针相同,除了您必须使用 std::static_pointer_cast
和 std::dynamic_pointer_cast
而不是 static_cast
和 dynamic_cast
。 C++17 将引入 reinterpret_cast
的对应项,但这对于向上或向下转换以及从 void 或到 void 的转换都不需要。
How do I dynamic upcast ... with smart pointers?
类型层次结构中的向上转换是隐式的,因此不需要强制转换:
struct A{
virtual ~A(){}
};
struct B:A{};
auto b = std::make_shared<B>();
std::shared_ptr<A> a = b;
How do I dynamic ... downcast with smart pointers?
如果您不确定源是否指向正确的类型,请使用 std::dynamic_pointer_cast
。这只有在继承是多态的(即基类至少有一个虚函数)时才有可能:
b = std::dynamic_pointer_cast<B>(a);
或std::static_pointer_cast
如果你知道类型是正确的:
b = std::static_pointer_cast<B>(a);
std::shared_ptr<void> b = std::dynamic_pointer_cast<void>(a); std::shared_ptr<int> c = std::dynamic_pointer_cast<int>(b);
void
和int
没有继承关系,不能向上转型或向下转型。
但是,所有指针都可以隐式转换为 void 指针,因此可以进行隐式转换:
std::shared_ptr<void> b = a;
并且,void 指针可以静态转换为原始类型,因此您可以使用 std::static_pointer_cast
:
std::shared_ptr<int> c = std::static_pointer_cast<int>(b);