~Kerify()只是断言警告我我做错了什么 在函数体中。 如果发生异常,验证不再相关。 有没有办法检测到我是(〜Verify())被 异常而不是结束范围? 即 ~确认() {if(!exception)assert(...); } 我知道我可以在函数中添加另一个变量,或者设置一些 的成员在我抛出之前验证,但是这使得使用验证类 作为测试函数完整性的简单方法。 I have a simple class that verifies some data between the beginning and theend of a function: class Verify{Verify() { note the data value }~Verify() { check if data value is the same as we noted }}; void foo(){Verify _v;...throw SomeException; // test can fail because _v is destroyed''prematurely''...}; ~Verify() just makes an assertion to warn me that I''ve done something wrongin the function body.If the exception occurs, the verification is no longer relevant.Is there some way to detect that I''m (~Verify()) being destroyed by anexception rather than the end of scope?i.e.~Verify(){ if (!exception) assert(...); } I know I could add another variable into the function, or set some member ofVerify before I throw, but that defeats the point of using the Verify classas a simple way to test the integrity of the function.推荐答案 Douglas Peterson <碲****** @ nospam.msn.com>在留言中写道 news:qN ******************** @ comcast.com ... "Douglas Peterson" <Te******@nospam.msn.com> wrote in messagenews:qN********************@comcast.com... I有一个简单的类来验证开头和之间的一些数据函数的结尾: 为什么不在函数中保持数据不变?或者在你的数据类中实现verify() 方法? 类验证 {验证(){注意数据值} 〜Verify(){检查数据值是否与我们记录的相同} }; void foo() {验证_v; ... 抛出SomeException; //测试可能会因为_v被破坏而失败''过早地'' ... }; 〜Verify()只是断言警告我我在函数体中做了一些错误的事情。如果发生异常,验证就不再相关了。有没有办法检测到我是谁(〜验证())是否被异常而不是范围的结束所破坏? 验证验证是否破坏了析构函数?你应该在这里重新考虑设计 。构造函数应该执行构造,而不是验证。 同样适用于析构函数。从ctor或者dor tor中抛出异常是一个很好的例子a_big_no_no。 ie 〜Verify() {if(!exception)断言(...);我知道我可以在函数中添加另一个变量,或者在我抛出之前设置一些成员的验证,但是这使得使用Verify 类作为测试函数完整性的简单方法。 I have a simple class that verifies some data between the beginning andthe end of a function:Why not keep data constant in the function instead? Or implement a verify()method in your data class otherwise? class Verify { Verify() { note the data value } ~Verify() { check if data value is the same as we noted } }; void foo() { Verify _v; ... throw SomeException; // test can fail because _v is destroyed ''prematurely'' ... }; ~Verify() just makes an assertion to warn me that I''ve done somethingwrong in the function body. If the exception occurs, the verification is no longer relevant. Is there some way to detect that I''m (~Verify()) being destroyed by an exception rather than the end of scope?To verify that Verify zapped your destructor? You should rethink the designhere. A constructor should be performing construction, not verification.Ditto for the destructor. Throwing exceptions from a ctor or d~tor is a goodexample of a_big_no_no. i.e. ~Verify() { if (!exception) assert(...); } I know I could add another variable into the function, or set some memberof Verify before I throw, but that defeats the point of using the Verifyclass as a simple way to test the integrity of the function. 你没有测试函数的完整性,你正在测试数据的 完整性。 You aren''t testing the integrity of the function, you are testing the data''sintegrity. 这只是一个断言,用于验证我的堆栈(不是程序堆栈)是否为当函数开始作为一个完整性检查时所处的状态相同 表示何时/如果代码被修改。 class验证 { public: Verify(){m_stackDepth = getStackDepth(); } ~Complete(){assert(m_stackDepth == getStackDepth();} int m_stackDepth; }; 这就是它的全部。我可以在操作堆栈的每个 函数的开头放一行: void foo() { 验证_verify; ... } 简单的馅饼。 显然我可以写: void foo() { int _stackDepth = getStackDepth(); ... 断言(_stackDepth == getStackDepth()); } 这似乎可以解决这个问题(因为断言会跳过 一次投掷),但很多大约60个使用它的函数有 备用返回路径。最好的解决方案是IMO将支票折入 类对象。 如果在执行函数期间发生抛出,则堆栈深度(再次 请记住我们是不谈论程序堆栈不同步是因为不再相关,整个堆栈因错误而被破坏 条件。 我当然可以保留它,但是当它没有任何意义时,它很烦人地获得断言 。 codigo <共**** @ codigo.trap.com>在消息中写道 新闻:我们***************** @ news20.bellglobal.com ... It''s just a assertion to verify that my stack (not the program stack) isleft in the same state it was in when the function began as a sanity checkfor when/if the code gets modified. class Verify{public:Verify() { m_stackDepth = getStackDepth(); }~Verify() { assert(m_stackDepth == getStackDepth(); }int m_stackDepth;}; That''s the whole of it. I can put one line at the beginning of everyfunction that manipulates the stack: void foo(){Verify _verify;...} Simple as pie. Obviously I could write: void foo(){int _stackDepth = getStackDepth();...assert(_stackDepth == getStackDepth());} That might seem to solve the problem (since the assertion would be skippedby a throw), but many of the roughly 60 functions that use this havealternate return paths. The best solution IMO was to toss the check into aclass object. If a throw occurs during execution of the function, the stack depth (againkeep in mind we''re not talking about the program stack) being out of sync isno longer relevant, the entire stack gets destroyed as a result of the errorcondition. I can just leave it as is of course, but it''s annoying getting the assertionwhen it has no meaning. "codigo" <co****@codigo.trap.com> wrote in messagenews:We*****************@news20.bellglobal.com... 道格拉斯彼得森 <碲****** @ nospam.msn.com>在消息中写道新闻:qN ******************** @ comcast.com ... "Douglas Peterson" <Te******@nospam.msn.com> wrote in message news:qN********************@comcast.com...我有一个简单的课验证开头和 I have a simple class that verifies some data between the beginning and之间的一些数据函数的结尾: 为什么不在函数中保持数据不变代替?或者在你的数据类中实现一个 verify()方法? Why not keep data constant in the function instead? Or implement a verify() method in your data class otherwise? 类验证 {验证( ){注意数据值} 〜Verify(){检查数据值是否与我们记录的相同} }; void foo() { 验证_v; ... 抛出SomeException; //测试可能会因为_v被破坏而失败''过早地'' ... }; 〜Verify()只是断言警告我我在函数体中做了一些 class Verify { Verify() { note the data value } ~Verify() { check if data value is the same as we noted } }; void foo() { Verify _v; ... throw SomeException; // test can fail because _v is destroyed ''prematurely'' ... }; ~Verify() just makes an assertion to warn me that I''ve done something 错误的。如果发生异常,则验证不再相关。是有一些方法来检测我(〜Verify())是否被异常而不是范围的结束所破坏? in the function body. If the exception occurs, the verification is no longer relevant. Is there some way to detect that I''m (~Verify()) being destroyed by an exception rather than the end of scope? 要验证验证是否已经破坏了你的析构函数?你应该重新考虑设计。构造函数应该执行构造,而不是验证。同样适用于析构函数。从ctor或dtor转出异常是一个很好的例子a_big_no_no。 To verify that Verify zapped your destructor? You should rethink the design here. A constructor should be performing construction, not verification. Ditto for the destructor. Throwing exceptions from a ctor or d~tor is a good example of a_big_no_no. ie 〜Verify() { if(!exception)assert(...);我知道我可以在函数中添加另一个变量,或者在我抛出之前设置 i.e. ~Verify() { if (!exception) assert(...); } I know I could add another variable into the function, or set some member验证的一些成员 使用Verify Verify before I throw, but that defeats the point of using the Verify 类作为测试函数完整性的简单方法。 as a simple way to test the integrity of the function. 你不是''测试功能的完整性,您正在测试数据的完整性。 You aren''t testing the integrity of the function, you are testing the data''s integrity. " ;道格拉斯彼得森 < Tergi ... @ nospam.msn.com>写道: "Douglas Peterson" <[email protected]> wrote: 如果发生异常,验证就不再相关了。有没有办法检测到我(@ Verify())被摧毁了异常而非范围结束? ie 〜Verify() {if(!exception)assert(...); } If the exception occurs, the verification is no longer relevant. Is there some way to detect that I''m (~Verify()) being destroyed byan exception rather than the end of scope? i.e. ~Verify() { if (!exception) assert(...); } 尝试uncaught_exception()(虽然这个函数被认为是 不优雅)。 AC Try uncaught_exception() (though this function is deemed to beunelegant). A.C. 这篇关于通过异常展开来检测我的析构函数是否被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-16 11:12