Results 1 to 5 of 5

Thread: virtual members

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default virtual members

    hi,
    Could anyone explain me about this warning? (and how take out it) thanks
    Qt Code:
    1. warning 'class Cri' has virtual functions but non-virtual destructor
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #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 members

    Quote Originally Posted by mickey
    Could anyone explain me about this warning?
    Actually it should be explained in any book that covers C++ basics.

    Qt Code:
    1. class A
    2. {
    3. public:
    4. A() {}
    5. virtual foo() {}
    6. ~A() {}
    7. };
    8.  
    9. class B : public A
    10. {
    11. public:
    12. B() {}
    13. virtual foo() {}
    14. ~B() {}
    15. };
    16.  
    17. ...
    18.  
    19. A *ptr = new B();
    20. ptr->foo(); // B::foo() will be called, since ptr is in fact B instance, not A
    21. delete ptr; // only A::~A() will be called, because destructor isn't virtual, hence the object won't be destructed properly.
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by mickey
    and how take out it
    Just read the error message and it will tell you what you need.

  3. #3
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: virtual members

    But it IS annoying that you still get the warning if you don't have a destructor at all. I often have to add an empty virtual destructor to avoid it.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  4. #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 members

    Quote Originally Posted by Michiel
    if you don't have a destructor at all.
    Then you have a default one, which isn't virtual.

  5. #5
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: virtual members

    Oh, never mind. I was arguing that the default destructor is empty, so doesn't have to be called. But the problem is, of course, that the destructor of the subclass needs to be called. If you don't make the superclass one virtual, that one gets called instead, empty or not.

    Still annoying, though. I'd be ok with making it default behaviour in the case of other virtual members.
    Last edited by Michiel; 13th August 2006 at 10:24. Reason: My mistake
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Similar Threads

  1. Qt/Embedded Installation error during make
    By mahe2310 in forum Installation and Deployment
    Replies: 5
    Last Post: 7th September 2006, 04:05
  2. linking libraries
    By JustaStudent in forum Newbie
    Replies: 29
    Last Post: 2nd May 2006, 08:30
  3. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 21:26
  4. Problems with Q_OBJECT and subclassing
    By renaissanz in forum Qt Programming
    Replies: 4
    Last Post: 21st February 2006, 22:18
  5. warning message on compile not understood
    By impeteperry in forum Qt Programming
    Replies: 13
    Last Post: 23rd January 2006, 23:36

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.