Results 1 to 6 of 6

Thread: 'new' in initialization list

  1. #1
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default 'new' in initialization list

    Hi,
    I am used to initialize pointers in my classes the following way:


    Qt Code:
    1. XX::XX():
    2. bla(new something(this)),
    3. bla2(new someotherthing(this))
    4. {
    5. //
    6. }
    To copy to clipboard, switch view to plain text mode 

    i.e. using 'new' to initialize member pointers in my classes. Usually Qt gui classes.
    With the gcc this works just fine, visual studio gives me a warning and I hate code, which does not compile cleanly without warnings.

    Now I wonder whether above code is acceptable and the VS warning is only a Microsoft marketing thing to support their 'managed' stuff, or really bad style. I searched the internet, but did not find much about above problem.

    The only problem I see is that if the construction of XX fails and an exception is thrown I have a memory leak since the destructor isn't called. However, with all the code I wrote up to date, I don't see this as problem, in all cases the program would have to terminate anyways.

    Something I overlook? Any comments?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: 'new' in initialization list

    Seems fine for me. Trolltech uses this approach in Qt as well...

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: 'new' in initialization list

    What is the warning?
    J-P Nurmi

  4. #4
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: 'new' in initialization list

    Quote Originally Posted by jpn View Post
    What is the warning?
    Arrgh, looks like I cannot reproduce it. In my current project I have absolutely no control over my build environment. Lately we changed from VS6 to VS8 and the build parameter change with almost every sync. <-- Only minor exaggeration. :-/

  5. #5
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'new' in initialization list

    It's C4355. And the warning is correct for someone who doesn't know what he does

  6. #6
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: 'new' in initialization list

    Quote Originally Posted by ChristianEhrlicher View Post
    It's C4355. And the warning is correct for someone who doesn't know what he does
    Thanks, that's it. Just googled a but. You are right, I only saw the possible memory leak as problem, missed totally that someone might use the this pointer before its object is fully constructed.

Similar Threads

  1. Single slot for multiple list boxes
    By Sheetal in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2008, 07:53
  2. Replies: 1
    Last Post: 22nd October 2007, 03:04
  3. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 20:25
  4. Replies: 26
    Last Post: 21st July 2007, 22:34
  5. Highlight text in list box
    By Sheetal in forum Qt Tools
    Replies: 7
    Last Post: 9th April 2007, 12:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.