更有效的C ++ [英] More effective C++

查看:59
本文介绍了更有效的C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了Scott Meyers的这本书并没有完全按照项目10进行


基本上他说构造函数应该捕获所有异常,做任何

需要清理然后扔掉。这是因为否则已经创建的成员

对象无法从外部销毁,因为

容器对象本身未完全创建。


看看这个 - (myclass包含class1和class2的对象)

类myclass {

class1 m_class1;

class2 m_class2;

public:

myclass(int c1,int c2):m_class1(c1),m_class2(c2){}

}


假设class1和class2都有构造函数,它们接受int

参数。

所以m_class1先被初始化,然后是m_class2。如果class2的构造函数

抛出异常,即使在自己清理之后,它会如何帮助?b $ b? m_class1已经构建完毕。它是如何被破坏的?


我实际上是在VC ++ 6编译器上尝试过它,它似乎有效。在中,

m_class1'的析构函数确实被调用了!我认为class1和class2

初始化是独立发生的,我们无法清理一个

,因为另一个例外。


如果我在这里遗漏了什么,请告诉我。

编译代码是如何知道m_class1是构造的并且需要销毁

,而它原样从m_class2传递异常。

I was just going through this book by Scott Meyers and did not fully
follow item 10.
Basically he says the constructor should catch all exceptions, do any
cleanups necessary and then throw. This is because otherwise the member
objects already created can not be destroyed from outside since the
container object itself is not fully created.

Look at this - (myclass contains objects of class1 and class2)
Class myclass {
class1 m_class1;
class2 m_class2;
public:
myclass(int c1, int c2):m_class1(c1), m_class2(c2) {}
}

Assume both class1 and class2 have constructors which take int
parameters.
so m_class1 gets initialized first and then m_class2. If the contructor
of class2 throws an exception, even after its own cleanup, how would it
help? m_class1 is already contructed. How is it destroyed?

I actually tried this on VC++ 6 compiler and it seems to work. As in ,
m_class1''s destructor indeed gets called! I think class1 and class2
initializations are independently happening and we can not cleanup one
because of an exception in the other.

Please let me know if I am missing something here. How does the
compiled code know that m_class1 is contructed and it needs to be
destroyed whereas it passes the exception from m_class2 as is.

推荐答案

vi ****** *****@yahoo.com 写道:

假设class1和class2都有构造函数,它们接受int
参数。
so m_class1首先初始化然后m_class2。如果class2的构造函数抛出异常,即使在自己清理之后,它会如何帮助? m_class1已经构建完毕。它是如何被破坏的?

Assume both class1 and class2 have constructors which take int
parameters.
so m_class1 gets initialized first and then m_class2. If the contructor
of class2 throws an exception, even after its own cleanup, how would it
help? m_class1 is already contructed. How is it destroyed?




编译器生成破坏它的代码。你需要做的清理

以响应构造函数中的异常,这是因为没有b $ b b的东西有适当的析构函数。


-


Pete Becker

Roundhouse Consulting,Ltd。



The compiler generates code that destroys it. The cleanup you need to do
in response to an exception in a constructor is for things that don''t
have appropriate destructors.

--

Pete Becker
Roundhouse Consulting, Ltd.


>你需要做的清理
> The cleanup you need to do
以响应构造函数中的异常是为了没有适当的析构函数。
in response to an exception in a constructor is for things that don''t
have appropriate destructors.




感谢您的回复。但我不得不反对一点。事实上,他在书中给出了清理代码的例子。
删除图像;

等,其中theImage是一个具有良好定义的对象dtor。


- 维克拉姆



Thanks for the response. But I will have to disagree a bit. Infact in
the example he has given in the book the cleanup code has
delete theImage;
etc where theImage is an object with a welldefined dtor.

--Vikram


>你需要做的清理
> The cleanup you need to do
以响应构造函数中的异常是为了没有适当的析构函数。
in response to an exception in a constructor is for things that don''t
have appropriate destructors.




感谢您的回复。但我不得不反对一点。事实上,他在书中给出了清理代码的例子。
删除图像;

等,其中theImage是一个具有良好定义的对象dtor。


- 维克拉姆



Thanks for the response. But I will have to disagree a bit. Infact in
the example he has given in the book the cleanup code has
delete theImage;
etc where theImage is an object with a welldefined dtor.

--Vikram


这篇关于更有效的C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆