473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why it is not good code for constructor

200 100+
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

Expand|Select|Wrap|Line Numbers
  1. C::C(int)
  2. try
  3.    : B(new int[n]) // horrible!
  4. {
  5.    ...
  6. }
  7. catch(Error &e)
  8. {
  9.  
  10. }
  11.  


thanks in advance,
George
Dec 26 '07 #1
2 1116
gpraghuram
1,275 Expert 1GB
If you dont have memory then only you will get a bad_alloc from the compiler.
If you are catching it and if it is being called from another constructor where you have inherited this you have handle the scenario in those places also.

I think that may be one of the reason for telling it a bad code

Raghuram
Dec 27 '07 #2
George2
200 100+
Hi Raghuram,


I agree that there are two situations as you mentioned. But I do not think it is bad code -- will release memory.

Here is my comments. Please feel free to correct me if I am wrong. :-)

1.

If you dont have memory then only you will get a bad_alloc from the compiler.
If there are bad_alloc, means no memory is allocated and no memory leak.

2.

If you are catching it and if it is being called from another constructor where you have inherited this you have handle the scenario in those places also.

I think that may be one of the reason for telling it a bad code

Raghuram
if there are exception (e.g. bad_alloc) in constructor, we just re-throw it until the outer-most constructor (outer-most wrapper), why it is bad code? More descriptions or pseudo code to express your ideas? :-)


regards,
George
Dec 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
29
by: Cheng Mo | last post by:
Recently I happens to read some sourcecode which has different style from my previous experience. In this style, all functions return a RETURN_TYPE_T which is defined as typedef unsigned int...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
8
by: nrhayyal | last post by:
hi all, is it a good idea to assign object to pointers without initialising it to NULL? suppose if i have a class TEST class ELEMENT { private: int a; string s1;
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
3
by: swengtoo | last post by:
In his book "More Effective C++", Scott Meyer suggests in "Item 4" to "Avoid gratuitous default constructors". To summarize his claims against default constructors for "the right classes" he...
11
by: optimistx | last post by:
http://developer.yahoo.com/yui/index.html Is this rubbish, evil, bad, wrong, not to recommend, "go away"-folks, "knitting suits them better"-people etc or something that a new javascript...
10
by: George2 | last post by:
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...
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.