Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 31,414 of 33,346    |
|    Ulrich Eckhardt to All    |
|    Re: Is destructor automatically be virtu    |
|    25 Aug 11 07:02:54    |
   
   ab334bdd   
   From: ulrich.eckhardt@dominolaser.com   
      
   linq936 wrote:   
   > Normally we do not declare constructor and destructor in pure class   
   > and compiler generates them, but since the class is pure, compiler   
   > should declare the destructor as virtual, isn't it?   
      
   No.   
      
      
   > class Base {   
   > public:   
   > virtual void do_sth() = 0;   
   > };   
      
   Other than a virtual destructor, you could also make it protected. Faulty   
   code like below then simply wouldn't compile.   
      
      
   > class D {   
   > public:   
   > virtual void do_sth() {}   
   >   
   > ... some other things ...   
   > };   
   >   
   > int main()   
   > {   
   > Base* p = new D();   
   > p->do_sth();   
   > delete p;   
   > return 0;   
   > }   
   >   
   > We write above sort of code a lot, if the destructor generated by   
   > compiler is not virtual, there will be memory leak.   
   >   
   > Can you confirm?   
      
   No, you will not get a memory leak but so-called "undefined behaviour",   
   which is much worse since it means that _anything_ can happen.   
      
      
   Uli   
      
   --   
   Domino Laser GmbH   
   Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932   
      
      
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca