Results 1 to 20 of 26

Thread: Basic question on new and delete

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    Quote Originally Posted by Codepoet
    ...because it went out of scope...
    This was an implementation bug What would you prefer in the following situation?
    Qt Code:
    1. MyModalDialog modalDlg;
    2. modalDlg.exec()
    3. if(modalDialog.getSomething()==true){
    4. //do something;
    5. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. MyModalDialog *modalDlg = new MyModalDialog;
    2. modalDlg->exec()
    3. if(modalDialog->getSomething()==true){
    4. //do something
    5. }
    6. delete modalDlg;
    To copy to clipboard, switch view to plain text mode 
    Then after code being added by many others...
    Qt Code:
    1. MyModalDialog *modalDlg = new MyModalDialog;
    2. modalDlg->exec()
    3. if(modalDialog->getSomething()==true){
    4. //do something
    5. }else{
    6. return; //Ouch
    7. }
    8. // Lot's of code that might also return somewhere
    9. delete modalDlg;
    To copy to clipboard, switch view to plain text mode 

    I'm not saying don't use pointers, but why not avoid them whem they are not needed?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Basic question on new and delete

    @yop: I cited not you but rh
    Yes one can (should?) avoid them if possible: Your example perfectly demonstrates it. It gets even worse when you work with exceptions.
    I never meant not using instances on the stack, that's most of the time ok but not always.

  3. #3
    Join Date
    Jan 2006
    Location
    Winnipeg, MB. Canada
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    The only real restriction to RAII-style initialized variables that i've encountered is, to state the obvious, that its type must be known at compile-time. Anything with runtime attributes (such as factory classes, etc.) still require pointers.

    My biggest qualm on using pointers? That damn "->" operator! C++ is *supposed* to be a language of type reduction, yet the arrow operator is 3x the keystrokes (don't forget the shift) as other dot-notated languages.

    If this makes me shallow (also i like ladies with big boobies), then so be it but in my opinion, RAII initialized variables are not only easier on the carpal tunnel, but are inherently safer (like GC without the overhead) and should be used whenever possible.

    I realize this is drifting slightly from the original post. If everyone is happy with me having the last word i'm good with that

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
  •  
Qt is a trademark of The Qt Company.