std::bad_cast 指针与参考情况
我注意到关于 std::bad_cast 异常,引用和指针的行为似乎不一样。例如:
class A { public: ~A() {} };
class B : public A {};
//Case #1
int main()
{
A a;
B& b = dynamic_cast<B&>(a); //Would throw std::bad_cast.
}
//Case #2
int main()
{
A* a = new A;
B* b = dynamic_cast<B*>(a); //Would not throw std::bad_cast.
}
在第一种情况下,会生成 std::bad_cast 异常,而在第二种情况下不会生成异常 - 相反,b 指针只是被赋予值 NULL。
有人可以向我解释为什么当两者都是 bad_cast 示例时只有前者抛出异常吗?我认为这个决定背后有一个很好的动机,但我误用了一些东西,因为我不理解这个动机。
I've noticed with regard to the std::bad_cast exception that references and pointers don't seem to act the same way. For example:
class A { public: ~A() {} };
class B : public A {};
//Case #1
int main()
{
A a;
B& b = dynamic_cast<B&>(a); //Would throw std::bad_cast.
}
//Case #2
int main()
{
A* a = new A;
B* b = dynamic_cast<B*>(a); //Would not throw std::bad_cast.
}
In the first case, an exception of std::bad_cast is generated, and in the second case no exception is generated - instead, the b pointer just is assigned the value NULL.
Can someone explain to me why only the former throws an exception when both are bad_cast examples? I figure there's a good motive behind the decision, and that I'm misusing something as I don't understand that motivation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是
dynamic_cast
被指定的行为方式:涉及指针的错误dynamic_cast
会产生空指针,但没有空引用,因此错误的dynamic_cast
> 涉及引用会引发bad_cast
。事实上,涉及指针的失败
dynamic_cast
会产生空指针,这一事实很有用,因为它允许进行更清晰、更简单的类型检查,并允许使用以下惯用法:使用此惯用法,
b
是当且仅当它非空时在范围内且可用。That is how
dynamic_cast
is specified to behave: a baddynamic_cast
involving pointers yields a null pointer, but there are no null references, so a baddynamic_cast
involving references throws abad_cast
.The fact that a failed
dynamic_cast
involving pointers yields a null pointer is useful because it allows for cleaner, simpler type checking and allows for the following idiom:With this idiom,
b
is in scope and usable if and only if it is non-null.引用必须绑定到包含有效内存地址的对象...它们不能“未初始化”,也没有默认的非绑定初始化值。请注意,C++11 标准中的第 8.5/8 节指出,
另一方面,指针变量只是内存地址,其中包含指向其他内存地址的值,因此可以具有 NULL 值。
因此,如果按照标准,
dynamic_cast
操作必须返回一个有效的内存地址来绑定到引用变量,那么如果动态转换失败,它就不能返回“非值” “...唯一的选择是抛出异常。References must be bound to an object that contains a valid memory address ... they cannot be "uninitialized", nor do they have a default non-bound initialization values. Note that section 8.5/8 in the C++11 standard states,
Pointer variables on the other-hand are just memory addresses that contain values that point to other memory addresses and therefore can have a
NULL
value.So if by the standard the
dynamic_cast<T&>
operation must return a valid memory address to bind to the reference variable, then if the dynamic cast fails, it can't return a "non-value" ... the only option is to throw an exception.