C++ 检查来自共享的强制转换\u ptr<;无效>;
我一直在尝试从一个C++ 检查来自共享的强制转换\u ptr<;无效>;,c++,c++11,casting,shared-ptr,smart-pointers,C++,C++11,Casting,Shared Ptr,Smart Pointers,我一直在尝试从一个共享的\u ptr中施放。我知道这是一个共享\u ptr 我能做些什么来检查静态指针\u cast是否工作正常? 或者以某种方式检查此共享的真正类型\u ptr? PD:我正在使用c++11,GCC4.8.2。如果您需要类型信息,我会对使用std::shared\u ptr感到奇怪 也许最好使用基于模板的方法(有人称之为it类型特征) 您应该为a和B创建一个公共基类(base)。然后可以使用静态指针转换(…)和动态指针转换将结果转换为A或B 通过向Base添加一个纯虚拟成员函数
共享的\u ptr
中施放。我知道这是一个共享\u ptr
我能做些什么来检查静态指针\u cast
是否工作正常?
或者以某种方式检查此共享的真正类型\u ptr
?
PD:我正在使用c++11,GCC4.8.2。如果您需要类型信息,我会对使用std::shared\u ptr
感到奇怪
也许最好使用基于模板的方法(有人称之为it类型特征)
您应该为a
和B
创建一个公共基类(base
)。然后可以使用静态指针转换(…)
和动态指针转换将结果转换为A
或B
通过向Base
添加一个纯虚拟成员函数dou things
,并在a
和B
中用覆盖它,可能会更好地避免动态强制转换。。。使用指向对象的指针执行操作
和。。。使用指向B对象的指针执行操作
“这是一个共享的\u ptr或一个共享的\u ptr”——这听起来像胡说八道。请修改你的问题。在点击“提交”按钮之前,使用预览来验证它是否符合您的预期。很抱歉输入错误。感谢您的编辑:-)没有从void**
到A*
的转换。这个问题似乎没有道理。A
和B
相关吗?如果是这样的话,为什么要一直到void*
?那么dynamic\u pointer\u cast
如何不符合您的要求呢?此外,正如Kerrek所说,它应该是共享的?您最好为a
和B
引入一个公共基类,并使用一个共享的ptr
,然后它可以动态的\u指针\u投射到适当的类型。
//x.valuePtr is a shared_ptr<void>
if(x.valuePtr is shared_ptr<A>){
... do things with the pointer to an A object
} else if (x.valuePtr is shared_ptr<B>){
... do things with the pointer to an B object
}
// If I know x.valuePtr is shared_ptr<A>
x.valuePtr = std::make_shared<A>();
// I can use static_pointer_cast somewhere else in the application and it works fine
std::shared_ptr<A> a_ptr = std::static_pointer_cast<A> (x.valuePtr);
// Same happens when I know it is a shared_ptr<B>
x.valuePtr = std::make_shared<B>();
std::shared_ptr<B> b_ptr = std::static_pointer_cast<B> (x.valuePtr);
// If x.valuePtr is shared_ptr<A>
x.valuePtr = std::make_shared<A>();
// But if I try to cast it to shared_ptr<B> somewhere in the application where it could be shared_ptr<A> or shared_ptr<B>
std::shared_ptr<B> b_ptr = std::static_pointer_cast<B> (x.valuePtr); // this does no fail
// It throws an exception when I try to use b_ptr. For example
b_ptr->AMethodInB();
x.valuePtr = std::make_shared<A>();
std::shared_ptr<B> b_ptr = std::static_pointer_cast<B> (x.valuePtr);
if(b_ptr == NULL || b_ptr.get() == NULL || !b_ptr){
"It never gets into this line"
"I would be able to try to cast it to shared_ptr<A>"
}
b_ptr->AMethodInB(); // and keeps failing here
struct A
{
int id;
};
struct B
{
int id1;
};
//Default template (neither A or B)
template<typename T>
struct Checker
{
enum { isA = 0, isB = 0 };
};
//Template for A
template<>
struct Checker<A>
{
enum { isA = 1, isB = 0 };
};
//Template for B
template<>
struct Checker<B>
{
enum { isA = 0, isB = 1 };
};
//Convenience function (only thing it does in runtime is compare 2 ints)
template<typename T>
bool checkA(std::shared_ptr<T> p)
{
return Checker<T>::isA == 1;
}
//Convenience function (only thing it does in runtime is compare 2 ints)
template<typename T>
bool checkB(std::shared_ptr<T> p)
{
return Checker<T>::isB == 1;
}
//Convenience function
template<typename T>
void test(std::shared_ptr<T> p)
{
std::cout
<< "is A : " << checkA(p)
<< " is B : " << checkB(p) << std::endl;
}
void test()
{
//This is working
std::shared_ptr<A> a = std::make_shared<A>();
std::shared_ptr<B> b = std::make_shared<B>();
test(a);
test(b);
//This is not working
std::shared_ptr<void> a1 = std::make_shared<A>();
std::shared_ptr<void> b1 = std::make_shared<B>();
test(a1);
test(b1);
}
is A : 1 is B : 0
is A : 0 is B : 1
is A : 0 is B : 0
is A : 0 is B : 0