Results 1 to 7 of 7

Thread: Virtual destructors

  1. #1
    Join Date
    Dec 2006
    Posts
    33
    Thanks
    10
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Virtual destructors

    This is an issue that has been bugging me for some time since starting to use Qt. Why do so many of the classes not have virtual destructors, since it seems clear that they may be used as base classes for derived classes?

    An obvious example is QWidget. Clearly this is a base class for many other classes, but according the documentation, the destructor is not virtual. My understanding has always been is that if a class can be a base class for other classes, the destructor should always be virtual. Is there some essential gap in my knowledge of C++ programming (surely not!)?

    Thanks for any input.
    Jim L
    Seattle, WA

  2. The following user says thank you to jml for this useful post:

    bunjee (1st November 2009)

  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Virtual destructors

    QWidget is derived from QObject which has a virtual destructor and once something has been declared virtual, it stays virtual.

  4. #3
    Join Date
    Dec 2006
    Posts
    33
    Thanks
    10
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Virtual destructors

    That begs the question as to why some classes, such as QTextEdit, do have virutual destructors.


    Example:

    Qt Code:
    1. class Foo
    2. {
    3. public:
    4. Foo()
    5. virtual ~Foo();
    6. };
    7. class Bar : public Foo
    8. {
    9. public:
    10. Bar();
    11. ~Bar();
    12. };
    13. class SubBar : public Bar
    14. {
    15. public:
    16. SubBar()
    17. ~SubBar();
    18. };
    19.  
    20. Foo *f1 = new Bar();
    21. Bar *b1 = new subBar();
    To copy to clipboard, switch view to plain text mode 

    Now when b1 is deleted, Bar's destructor is invoked since Foo's destructor is virtual. However when b1 is deleted, SubBar's destructor is NOT invoked, since Bar's destructor is not virtual. This strikes me as a problem.
    Jim L
    Seattle, WA

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Virtual destructors

    Quote Originally Posted by jml View Post
    That begs the question as to why some classes, such as QTextEdit, do have virutual destructors.
    All widgets do have a virtual destructor, because they inherit QObject which has a virtual destructor.

  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: Virtual destructors

    Quote Originally Posted by jml View Post
    That begs the question as to why some classes, such as QTextEdit, do have virutual destructors.


    Example:

    Qt Code:
    1. class Foo
    2. {
    3. public:
    4. Foo()
    5. virtual ~Foo();
    6. };
    7. class Bar : public Foo
    8. {
    9. public:
    10. Bar();
    11. ~Bar();
    12. };
    13. class SubBar : public Bar
    14. {
    15. public:
    16. SubBar()
    17. ~SubBar();
    18. };
    19.  
    20. Foo *f1 = new Bar();
    21. Bar *b1 = new subBar();
    To copy to clipboard, switch view to plain text mode 

    Now when b1 is deleted, Bar's destructor is invoked since Foo's destructor is virtual. However when b1 is deleted, SubBar's destructor is NOT invoked, since Bar's destructor is not virtual. This strikes me as a problem.
    Did you try the example you gave here ? Just put some cout/qDebugs and you will be able to confirm that deleting b1 calls destructor of SubBar.
    ~Bar is virtual since it inherits Foo which has virtual destructor.

    EDIT:
    Just for your info
    Qt Code:
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. class Foo
    5. {
    6. public:
    7. Foo() { cout << "Foo" << endl; }
    8. virtual ~Foo() { cout << "~Foo" << endl; }
    9. };
    10. class Bar : public Foo
    11. {
    12. public:
    13. Bar() { cout << "Bar" << endl; }
    14. ~Bar() { cout << "~Bar" << endl; }
    15. };
    16. class SubBar : public Bar
    17. {
    18. public:
    19. SubBar() { cout << "SubBar" << endl; }
    20. ~SubBar() { cout << "~SubBar" << endl; }
    21. };
    22.  
    23. int main()
    24. {
    25. Foo *f1 = new Bar();
    26. Bar *b1 = new SubBar();
    27. delete b1;
    28. return 0;
    29. }
    To copy to clipboard, switch view to plain text mode 

    Output
    Foo
    Bar
    Foo
    Bar
    SubBar
    ~SubBar
    ~Bar
    ~Foo
    Last edited by Gopala Krishna; 14th January 2008 at 20:32.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. The following user says thank you to Gopala Krishna for this useful post:

    jml (14th January 2008)

  8. #6
    Join Date
    Dec 2006
    Posts
    33
    Thanks
    10
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Virtual destructors

    Ugh! You're right. I did have a basic misunderstanding on how virtual destrutors work.

    Thanks.
    Jim L
    Seattle, WA

  9. #7
    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: Virtual destructors

    Quote Originally Posted by jml View Post
    That begs the question as to why some classes, such as QTextEdit, do have virutual destructors.
    Because different people write the code and some of them redeclare methods as virtual and others not.

  10. The following user says thank you to wysota for this useful post:

    bunjee (1st November 2009)

Similar Threads

  1. Cost of pure virtual
    By ShaChris23 in forum General Programming
    Replies: 4
    Last Post: 4th November 2007, 18:20
  2. QPluginLoader not recognizing a plugin
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 29th June 2007, 14:13
  3. Compiling error
    By vfernandez in forum Newbie
    Replies: 2
    Last Post: 9th March 2007, 21:02
  4. inheritance, pure virtual
    By TheKedge in forum General Programming
    Replies: 2
    Last Post: 18th January 2007, 11:20
  5. link error for visual studio.net 2003
    By berlin in forum Newbie
    Replies: 9
    Last Post: 29th September 2006, 16:06

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.