使用dynamic_cast转换指针时抛出bad_cast?

发布于 2024-11-29 09:04:03 字数 247 浏览 1 评论 0原文

如果您强制转换引用,dynamic_cast 会抛出 bad_cast 异常,但据我所知,标准指针被视为引用,即指针是引用的类型。
那么在转换指针时我应该得到 bad_cast 吗?

这个问题是由此页面的try-catch块引起的。这个 try-catch 块不合适吗?

dynamic_cast throws bad_cast exception if you cast a reference but as I know in the standard pointers are considered as references, i.e. a pointer is a type of a reference.
So should I get bad_cast when casting pointers?

This question arose from the try-catch block from this page. Is this try-catch block inappropriate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲性难收 2024-12-06 09:04:03

否,如果出现错误的转换,dynamic_cast 将返回 null
另外,dynamic_cast仅适用于多态类,因此,如果您正在谈论内置数据类型(来自问题中的链接),那么您应该使用static_cast

顺便说一句,引用不是指针。

No with pointers in case of a bad cast, dynamic_cast will return a null.
Also, dynamic_cast works only on Polymorphic classes, So if you are talking about built in data types(from the link in your question) then static_cast is what you should be using.

And btw, References are NOT pointers.

最冷一天 2024-12-06 09:04:03

关于原来的问题“那么在转换指针时我应该得到 bad_cast 吗?”,不。

这就是为什么你可以看到类似的结构

if( T* pT = dynamic_cast<T*>( p ) ) ...  // use pT in if body

关于新问题“这个 try-catch 块不合适吗?”,不,它是一个 try< /code>-catch 块来捕获分配错误;它与 dynamic_cast 本身无关。

干杯&呵呵,

Regarding the original question "So should I get bad_cast when casting pointers?", No.

That's why you can see constructions like

if( T* pT = dynamic_cast<T*>( p ) ) ...  // use pT in if body

Regarding the new question "Is this try-catch block inappropriate?", no, it is a try-catch block to catch allocation errors; it's not related to the dynamic_cast as such.

Cheers & hth.,

差↓一点笑了 2024-12-06 09:04:03

由于 dynamic_cast 不会引发异常,因此完全不需要 try-catch 块之一。

但是,您可以轻松定义自己的强制转换函数,该函数确实会引发指针异常。这就是我在自己的代码中所做的事情(简化的代码,请注意,我使用垃圾收集,这样我就可以抛出指针而不会产生任何后果):

template <class Class, class Object>
inline Class* cast (Object* obj)
{
    Class* result = dynamic_cast<Class*>(obj);
    if( obj != null and result == null ){
        throw new ClassCastException(); //  unusual, throw bad_cast if you prefer
    }
    return result;
}

template <class Class, class Object>
inline const Class* cast (const Object* obj)
{
    const Class* result = dynamic_cast<const Class*>(obj);
    if( obj != null and result == null ){
        throw new ClassCastException(); //  unusual, throw bad_cast if you prefer
    }
    return result;
}

顺便说一句,我也将它用作 static_cast 的语法糖,但这只是可能是因为我不使用它来dynamic_cast引用和const引用:

template <class Class, class Object>
inline Class cast (const Object& obj)
{
    return static_cast<Class>(obj);
}

我想说你最好实现自己的转换函数,该函数在异常处理方面是一致的,并且精确地执行你想要和期望的操作。我做到了,并且再也没有回头。

Since dynamic_cast<T*> does not throw exceptions, one of the try-catch blocks are completely unnecessary.

However you can easily define your own cast function that does indeed throw exception for pointers. That is what I am doing in my own code (simplified code, and mind you I use garbage collection so I can throw pointers without consequence):

template <class Class, class Object>
inline Class* cast (Object* obj)
{
    Class* result = dynamic_cast<Class*>(obj);
    if( obj != null and result == null ){
        throw new ClassCastException(); //  unusual, throw bad_cast if you prefer
    }
    return result;
}

template <class Class, class Object>
inline const Class* cast (const Object* obj)
{
    const Class* result = dynamic_cast<const Class*>(obj);
    if( obj != null and result == null ){
        throw new ClassCastException(); //  unusual, throw bad_cast if you prefer
    }
    return result;
}

On a side note, I also use it as a syntactic sugar for static_cast, but this is only possible because I do not use it to dynamic_cast references and const references:

template <class Class, class Object>
inline Class cast (const Object& obj)
{
    return static_cast<Class>(obj);
}

I'd say you are better off implementing your own casting function that is coherent in terms of exception handling and do precisely what you want and expect. I did and never looked back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文