English 中文(简体)
C++方案能否很好地运作,
原标题:Can a C++ program work well even though an exception is thrown from a class?

这是一个面试问题 面试已经完成了

Given a class A with members of Class B and C. If an exception happens in class C s constructor but the program can still work well, what is the reason ?

我的回答是:

A.或A没有指示在C类进行某些操作。

C类没有任何即时处理。

例外不是一个错误。 例外处理器功能处理良好 。

有什么更好的主意吗?

谢谢!

问题回答

如果 >C 是类的可选成员 A , 则在 C 实例上有一个无效值指针是确定。 那么, 假设适当的例外处理不会有什么问题 。

我认为它指的是这个语法:

 A::A() try : B(...),C(...) 
 {
    //A constructor body
 }
 catch(...) {}

<强度 > EDIT 永远不要介意,捕获区块中未明确重新重新投入的每个例外都是自动重新投出

"http://www.gotw.ca/gotw/066.htm" rel="no follow" > this

要成功构建 A 对象, 您需要成功构建其成员( 类型为 B C )。 如果程序 wokring 正确, 这意味着它可以从创建 < code> A 对象失败中恢复 。

程序必须抓住从 < code> A 构建器丢弃的例外, 并以某种方式处理错误情形 。

例如,您可以将一套不同的参数传递到 A s 构建器(该构建器又将不同的参数传递到其 C 成员驱动器,该驱动器现在不投掷),例如基于其他配置值。

或者还有一条解决最初问题的替代途径,它不涉及创建类型 < code> A (例如,这一替代途径可能更昂贵,因此计算费用可能更高,这可能是它不是首选原因)。

该方案如果设计成可以继续运作,但A类物体的构造必须失败,因为如果一个物体的基地或成员不能初始化,则不可能完全建造该物体。

一种类别有可能用自有的指针持有物体,而且如果期望不会从不尾品清单中逃脱,则可以在没有被持有的物体的情况下建造该物体。

struct C {
    C();
};

struct A {
    A();
    std::unique_ptr<C> c;
};

A::A() {
    try {
        c.reset(new C);
    }
    catch (...){
        // oops. Can t re-throw, could log
    }
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签