为什么它不是构造函数的好代码 [英] Why it is not good code for constructor

查看:73
本文介绍了为什么它不是构造函数的好代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

以下是Dr. Dobb C ++的样本。在分析中,代码不好

以下。


但我不认为代码不好,


1.如果在新的int []中抛出bad_alloc,我们只是抓住它并写下

一些日志;

2.如果B中有任何异常''的构造函数,我们也将在

catch块中,我们也可以写一些日志。


为什么代码不好?任何评论?


(我不同意有资源泄漏,因为如果我们在new int []中遇到

bad_alloc,就没有内存完全分配,所以没有内存/资源泄漏的根据。
http://www.ddj.com/cpp/184401297

Hello everyone,
Here is a sample from Dr. Dobb C++. In the analysis, the code is bad
below.

But I do not think the code is bad,

1. if bad_alloc is thrown in new int[], we just catch it and write
some log;
2. if there are any exception in B''s constructor, we will also be in
catch block and we could also write some log.

Why it is bad code? Any comments?

(I do not agree that there is resource leak, since if we met with
bad_alloc in new int[], there is no memory allocated at all, so no
root of memory/resource leak).
http://www.ddj.com/cpp/184401297

展开 | 选择 | 换行 | 行号

推荐答案

2007-12-26 09:10:47 -0500,George2< ge ************* @ yahoo.comsaid:
On 2007-12-26 09:10:47 -0500, George2 <ge*************@yahoo.comsaid:

您好每个人,


以下是Dr. Dobb C ++的样本。在分析中,代码不好

以下。


但我不认为代码不好,


1.如果在新的int []中抛出bad_alloc,我们只是抓住它并写下

一些日志;

2.如果B中有任何异常''的构造函数,我们也将在

catch块中,我们也可以写一些日志。


为什么代码不好?任何评论?


(我不同意有资源泄漏,因为如果我们在new int []中遇到

bad_alloc,就没有内存完全分配,所以没有内存/资源泄漏的根据。
http://www.ddj.com/cpp/184401297

Hello everyone,
Here is a sample from Dr. Dobb C++. In the analysis, the code is bad
below.

But I do not think the code is bad,

1. if bad_alloc is thrown in new int[], we just catch it and write
some log;
2. if there are any exception in B''s constructor, we will also be in
catch block and we could also write some log.

Why it is bad code? Any comments?

(I do not agree that there is resource leak, since if we met with
bad_alloc in new int[], there is no memory allocated at all, so no
root of memory/resource leak).
http://www.ddj.com/cpp/184401297

展开 | 选择 | 换行 | 行号


2007年12月26日星期三09:34:24 -0500,Kira Yamato写道:
On Wed, 26 Dec 2007 09:34:24 -0500, Kira Yamato wrote:

2007-12-26 09:10:47 - 0500,George2< ge *** **********@yahoo.comsidid:
On 2007-12-26 09:10:47 -0500, George2 <ge*************@yahoo.comsaid:

>大家好,

以下是博士的样本.Dobb C ++。在分析中,代码不好


但我不认为代码不好,

1。如果在new int []中抛出bad_alloc,我们只是抓住它并写一些
日志;
2。如果B'的构造函数中有任何异常,我们也会在
catch块中,我们也可以写一些日志。

为什么代码不好?有什么意见吗?

(我不同意资源泄漏,因为如果我们在新的int []中遇到了bad_alloc,根本没有分配内存,所以没有root <内存/资源泄漏)。
http:// www。 ddj.com/cpp/184401297

>Hello everyone,
Here is a sample from Dr. Dobb C++. In the analysis, the code is bad
below.

But I do not think the code is bad,

1. if bad_alloc is thrown in new int[], we just catch it and write some
log;
2. if there are any exception in B''s constructor, we will also be in
catch block and we could also write some log.

Why it is bad code? Any comments?

(I do not agree that there is resource leak, since if we met with
bad_alloc in new int[], there is no memory allocated at all, so no root
of memory/resource leak).
http://www.ddj.com/cpp/184401297

展开 | 选择 | Wrap | 行号





George2写道:


George2 wrote:

大家好,


这是Dr. Dobb C ++的样本。在分析中,代码不好

以下。


但我不认为代码不好,


1.如果在新的int []中抛出bad_alloc,我们只是抓住它并写下

一些日志;

2.如果B中有任何异常''的构造函数,我们也将在

catch块中,我们也可以写一些日志。


为什么代码不好?任何评论?


(我不同意有资源泄漏,因为如果我们在new int []中遇到

bad_alloc,就没有内存完全分配,所以没有内存/资源泄漏的根据。
http://www.ddj.com/cpp/184401297

Hello everyone,
Here is a sample from Dr. Dobb C++. In the analysis, the code is bad
below.

But I do not think the code is bad,

1. if bad_alloc is thrown in new int[], we just catch it and write
some log;
2. if there are any exception in B''s constructor, we will also be in
catch block and we could also write some log.

Why it is bad code? Any comments?

(I do not agree that there is resource leak, since if we met with
bad_alloc in new int[], there is no memory allocated at all, so no
root of memory/resource leak).
http://www.ddj.com/cpp/184401297

展开 | 选择 | 换行 | 行号


这篇关于为什么它不是构造函数的好代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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