问题描述
最近,一名面试官问我 C ++中的异常对象在哪里分配,堆或堆栈?我不确定,但是我回答了堆栈,因为我认为没有"new"或"malloc" .正确吗?
Recently an interviewer asked me where the exception object in C++ is allocated, heap or stack? I'm not sure but I answered stack since I thought there is no "new" or "malloc". Is it correct?
然后他一直问我如果它在堆栈上,假设A类抛出异常对象,我们说"e",而B类捕获"e".由于"e"在A的堆栈中,那么B如何访问该"e"?
我对第二个问题不太清楚.谁能给我一些示例代码,说明"A类抛出e而B类捕获了它"?另外,我猜想B可以通过复制值或地址来捕获e,但是访问者只是拒绝我的答案而没有给我一个正确的答案,那么正确的答案是什么,是否有任何机制可以确保类对象可以捕获其他类对象的异常?谢谢〜
I'm not very clear about the second question. Could anyone can give me some example code showing that "class A throw e and class B catch it"? Also, I guessed B can catch e by copying the value or address but the interviewer only denied my answer without giving me right one, so what is the right answer, is there any mechanism can ensure class object can catch exceptions from other class objects? Thanks~
推荐答案
来自[except.throw]/15.1/4:
From [except.throw]/15.1/4:
最终参考文献[basic.stc.dynamic.allocation]/4说:
The final reference, [basic.stc.dynamic.allocation]/4, says:
这篇关于异常对象在哪里有其空间,堆或堆栈,以及如何在不同的类中访问它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!