CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Why it is not good code for constructor

    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

    Code:
    C::C(int)
    try
       : B(new int[n]) // horrible!
    {
       ...
    }
    catch(Error &e)
    {
       
    }


    thanks in advance,
    George

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Why it is not good code for constructor

    This question has been answered (or an attempt has been made to answer it ) elsewhere.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured