Results 1 to 8 of 8

Thread: Confused - QGraphicsItem destructor

  1. #1
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    84
    Thanks
    5
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Confused - QGraphicsItem destructor

    Hi guys,

    I'm sort of confused right now. Let's say I have created a QGraphicsLineItem(), which will eventually have children (all sorts of graphics items, doesn't really matter). Now I want to explicitly delete my object:
    Qt Code:
    1. //...
    2. //...
    3. delete line;
    4. line = 0;
    To copy to clipboard, switch view to plain text mode 

    Now the part I don't get is that in the Qt sources the destructor of QGraphicsLineItem is empty:
    Qt Code:
    1. {
    2. }
    To copy to clipboard, switch view to plain text mode 

    QGraphicsLineItem is, of course, a sub-class of QGraphicsItem, which has the destructor defined with code to delete all its children.
    So, how is it, that if I delete my QGraphicsLineItem that it will also delete all its children (i.e. *label)? The base class destructor QGraphicsItem::~QGraphicsItem() is declared as virtual, thus a destructor wouldn't be needed for QGraphicsLineItem, right? And by declaring an own destructor for QGraphicsLineItem which is empty, the base class destructor doesn't get called, right?

    Or am I missing something here?
    Thanks.

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

    Default Re: Confused - QGraphicsItem destructor

    Quote Originally Posted by durbrak View Post
    And by declaring an own destructor for QGraphicsLineItem which is empty, the base class destructor doesn't get called, right?
    Well actually, the base class destructor gets called right after sub class destructor.
    J-P Nurmi

  3. #3
    Join Date
    Oct 2006
    Location
    Germany
    Posts
    84
    Thanks
    5
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Confused - QGraphicsItem destructor

    So, it's a feature of C++ to call every single destructor from the sub-class up to the base-class?
    And why is a destructor for QGraphicsLineItem() defined? Wouldn't the base-class destructor be called if there was no destructor in the sub-class?

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

    Default Re: Confused - QGraphicsItem destructor

    Quote Originally Posted by durbrak View Post
    So, it's a feature of C++ to call every single destructor from the sub-class up to the base-class?
    Yup. That's the reverse order of constructors. Notice that even if you don't explicitly call a base class constructor the default one gets called.

    And why is a destructor for QGraphicsLineItem() defined?
    Some C++ programmers tend to define destructors even if they're empty. This makes it clear that the destructor wasn't simply forgotten. Usually you need destructors in C++ more often than what you need when working with Qt, thanks to certain automatic cleanups.

    Wouldn't the base-class destructor be called if there was no destructor in the sub-class?
    Yeah, it would.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    durbrak (11th February 2008)

  6. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Confused - QGraphicsItem destructor

    Quote Originally Posted by durbrak View Post
    And why is a destructor for QGraphicsLineItem() defined?
    I feel they do this also so that they can do additional cleanups later without breaking binary compatibility
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

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

    Default Re: Confused - QGraphicsItem destructor

    Quote Originally Posted by Gopala Krishna View Post
    I feel they do this also so that they can do additional cleanups later without breaking binary compatibility
    Introducing a new function in class interface does not break ABI. Thus, you can later add a destructor if needed. But once added you can't later remove it. So I'd say the opposite; perhaps there was earlier something to cleanup.
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    Gopala Krishna (12th February 2008)

  9. #7
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Confused - QGraphicsItem destructor

    Oh ya no ? I got it wrong.
    Thanks for correcting me
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #8
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Confused - QGraphicsItem destructor

    Adding a reimplementation of a virtual function doesn't break BC, but it also doesn't take effect until the source is recompiled. So if you need to fix a bug by adding functionality in a reimplementation, you're not actually fixing existing apps. By defining the function from the start, we know that the destructor is called by the application already, so we can safely add code and rely on it being called. Otoh, if we add a reimplementation, existing compilation units will still call the "old" implementation; they won't call the new implementation until the app is recompiled.

    Because of that, it's best for a library to always reimplement virtual functions in subclasses, even if the implementation is an empty destructor, or if it just calls the base implementation. (Note though that not all classes in Qt do this).
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  11. The following user says thank you to Bitto for this useful post:

    jpn (13th February 2008)

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 11:31

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.