dynamic_pointer_cast 意外行为
dynamic_pointer_cast unexpected behaviour
我正在建工厂 class,我需要 return 从 unique_ptr
到 BaseClass
。 returned 指针由 DerivedClass
对象组成,该对象使用 make_shared
转换为共享指针,然后转换为所需的 BaseClass
指针:
#include "BaseClass.h"
#include "DerivedClass.h"
std::unique_ptr<BaseClass> WorkerClass::DoSomething()
{
DerivedClass derived;
// Convert object to shared pointer
auto pre = std::make_shared<DerivedClass>(derived);
// Convert ptr type to returned type
auto ret = std::dynamic_pointer_cast<BaseClass>(ptr);
// Return the pointer
return std::move(ret);
}
我在 std::move
上遇到这个编译器错误
error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::nullptr_t'
1> with
1> [
1> _Ty=rfidaccess::BaseClass
1> ]
1> nullptr can only be converted to pointer or handle types
1>c:\project\dev\traansite1r\traansite1rcommon\tag.cpp(261): error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::nullptr_t'
1> with
1> [
1> _Ty=rfidaccess::BaseClass
1> ]
1> nullptr can only be converted to pointer or handle types
1>c:\project\dev\traansite1r\traansite1rcommon\tag.cpp(337): error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'rfidaccess::AARLocomotiveBaseClass' to 'std::nullptr_t'
1> with
1> [
1> _Ty=rfidaccess::BaseClass
1> ]
1> nullptr can only be converted to pointer or handle types
1>c:\project\dev\traansite1r\traansite1rcommon\tag.cpp(393): error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'rfidaccess::AAREndOfTrainBaseClass' to 'std::nullptr_t'
1> with
1> [
1> _Ty=rfidaccess::BaseClass
1> ]
1> nullptr can only be converted to pointer or handle types
我正在使用 VS2012...
为什么它使用的东西与声明的 (std::unique_ptr<BaseClass>
) 不同?
dynamic_pointer_cast
不是 return 正在 std::unique_ptr<BaseClass>
ret 吗?
帮助了解发生了什么。
std::shared_ptr
无法转换为 unique_ptr
。
在你的情况下,你只需要以下内容:
std::unique_ptr<BaseClass> WorkerClass::DoSomething()
return std::make_unique<DerivedClass>(/*args*/);
}
我正在建工厂 class,我需要 return 从 unique_ptr
到 BaseClass
。 returned 指针由 DerivedClass
对象组成,该对象使用 make_shared
转换为共享指针,然后转换为所需的 BaseClass
指针:
#include "BaseClass.h"
#include "DerivedClass.h"
std::unique_ptr<BaseClass> WorkerClass::DoSomething()
{
DerivedClass derived;
// Convert object to shared pointer
auto pre = std::make_shared<DerivedClass>(derived);
// Convert ptr type to returned type
auto ret = std::dynamic_pointer_cast<BaseClass>(ptr);
// Return the pointer
return std::move(ret);
}
我在 std::move
error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::nullptr_t'
1> with
1> [
1> _Ty=rfidaccess::BaseClass
1> ]
1> nullptr can only be converted to pointer or handle types
1>c:\project\dev\traansite1r\traansite1rcommon\tag.cpp(261): error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::nullptr_t'
1> with
1> [
1> _Ty=rfidaccess::BaseClass
1> ]
1> nullptr can only be converted to pointer or handle types
1>c:\project\dev\traansite1r\traansite1rcommon\tag.cpp(337): error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'rfidaccess::AARLocomotiveBaseClass' to 'std::nullptr_t'
1> with
1> [
1> _Ty=rfidaccess::BaseClass
1> ]
1> nullptr can only be converted to pointer or handle types
1>c:\project\dev\traansite1r\traansite1rcommon\tag.cpp(393): error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'rfidaccess::AAREndOfTrainBaseClass' to 'std::nullptr_t'
1> with
1> [
1> _Ty=rfidaccess::BaseClass
1> ]
1> nullptr can only be converted to pointer or handle types
我正在使用 VS2012...
为什么它使用的东西与声明的 (std::unique_ptr<BaseClass>
) 不同?
dynamic_pointer_cast
不是 return 正在 std::unique_ptr<BaseClass>
ret 吗?
帮助了解发生了什么。
std::shared_ptr
无法转换为 unique_ptr
。
在你的情况下,你只需要以下内容:
std::unique_ptr<BaseClass> WorkerClass::DoSomething()
return std::make_unique<DerivedClass>(/*args*/);
}