Results 1 to 14 of 14

Thread: Destructor overriding

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Destructor overriding

    As long as the method is not virtual -- yes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Feb 2013
    Posts
    9
    Qt products
    Qt4 Qt5 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Destructor overriding

    Quote Originally Posted by wysota View Post
    As long as the method is not virtual -- yes.
    Why? It seems to me that destructor overriding might prevent potential code duplication if one would need to delete arrows in other places. It will also prevent from potential repeated deletion, if one deleted an arrow, but forgot to clean the pointers in endItem() startItem() and objects. Cleaning of the arrow pointers in outer objects must occur every time the arrow is deleted, so why not implement it in destructor?
    Overriding of destructor will allow to make Arrow::stratItem() and Arrow::endItem() methods private.
    Last edited by Daylight; 28th February 2013 at 15:57.

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

    Default Re: Destructor overriding

    Quote Originally Posted by Daylight View Post
    Why?
    Why what?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2013
    Posts
    9
    Qt products
    Qt4 Qt5 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Destructor overriding

    Why is it better to implement separate DiagramItem::removeArrows() function with explicit pointer cleaning outside of Arrow object (like it is done in Diagram Scene Example)?
    Last edited by Daylight; 28th February 2013 at 16:09.

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

    Default Re: Destructor overriding

    I didn't say it was better or worse. Both are acceptable solutions as far as they serve your goal. As long as you don't call a virtual method on oneself from its own destructor, everything is fine. Consider the fact that QGraphicsItem has its own destructor that will remove all its children and remove itself from the scene. It will not detach it from items that are not part of its parent-child relationship. Your destructor has this problem that it an arrow is not attached to an item (e.g. the item is destroyed before the arrow is removed), your program will crash. In the original code an arrow can function without an object attached to it since it is a "stupid" object that doesn't care about its environment.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2013
    Posts
    9
    Qt products
    Qt4 Qt5 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Destructor overriding

    Quote Originally Posted by wysota View Post
    I didn't say it was better or worse. Both are acceptable solutions as far as they serve your goal. As long as you don't call a virtual method on oneself from its own destructor, everything is fine. Consider the fact that QGraphicsItem has its own destructor that will remove all its children and remove itself from the scene. It will not detach it from items that are not part of its parent-child relationship. Your destructor has this problem that it an arrow is not attached to an item (e.g. the item is destroyed before the arrow is removed), your program will crash. In the original code an arrow can function without an object attached to it since it is a "stupid" object that doesn't care about its environment.
    If the item is destroyed before the arrow is removed, the program will crash anyway, because the other item will try to access destroyed item via Arrow::start/endItem(). It is not possible, though, because every item stores the list of attached arrows and removes them before destruction.
    But, youre right, it seems it is better to make arrow a child of this->startItem(), this is more safe. Example's objects architecture seems a bit odd to me...
    Last edited by Daylight; 28th February 2013 at 16:57.

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

    Default Re: Destructor overriding

    No, making the arrow a child of any item is not a good idea. Think what happens if you try to rotate or scale such item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2013
    Posts
    9
    Qt products
    Qt4 Qt5 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Destructor overriding

    Quote Originally Posted by wysota View Post
    No, making the arrow a child of any item is not a good idea. Think what happens if you try to rotate or scale such item.
    It should be fine. Arrows rendering is treated in Arrow:: paint(), so it should be ok.

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

    Default Re: Destructor overriding

    It will not be fine. The arrow will be rotated or scaled as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Feb 2013
    Posts
    9
    Qt products
    Qt4 Qt5 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Destructor overriding

    Quote Originally Posted by wysota View Post
    It will not be fine. The arrow will be rotated or scaled as well.
    Why would it be? It will be repainted (correctly) each time the parent object is changed. The painting is done with respect to start/endItem() coordinates using the mapFromItem(myStart/endItem, 0, 0).

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

    Default Re: Destructor overriding

    Quote Originally Posted by Daylight View Post
    The painting is done with respect to start/endItem() coordinates using the mapFromItem(myStart/endItem, 0, 0).
    Coordinates of the items will not change if they are rotated or scaled. If you counter that then you counter everything GraphicsView stands for. The arrow simply can't be a child of an item if it is supposed to be independent of the item. Besides it is attached to two items and it can't have two parents.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Feb 2013
    Posts
    9
    Qt products
    Qt4 Qt5 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Destructor overriding

    Quote Originally Posted by wysota View Post
    Coordinates of the items will not change if they are rotated or scaled. If you counter that then you counter everything GraphicsView stands for. The arrow simply can't be a child of an item if it is supposed to be independent of the item. Besides it is attached to two items and it can't have two parents.
    From the logical point of view, arrow may be a child of an item it starts from. It is not completely independent, it should always be connected to two items. Arrows can not be moved directly, only by moving start/end Items.
    This is from the example:
    Qt Code:
    1. void Arrow::updatePosition()
    2. {
    3. QLineF line(mapFromItem(myStartItem, 0, 0), mapFromItem(myEndItem, 0, 0));
    4. setLine(line);
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Overriding Proxies
    By jdgrant in forum Qt Programming
    Replies: 0
    Last Post: 25th November 2011, 01:23
  2. Overriding global new
    By branko in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2010, 16:10
  3. overriding QListWidget advice
    By codebehind in forum Qt Programming
    Replies: 3
    Last Post: 28th September 2010, 22:39
  4. Overriding drawRubberBand()
    By andrew.nguyen in forum Qwt
    Replies: 3
    Last Post: 21st April 2010, 06:58
  5. Overriding shortcuts for QGraphicsItems
    By pherthyl in forum Qt Programming
    Replies: 3
    Last Post: 16th May 2008, 22:47

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.