From: Ron Natalie on 6 Mar 2007 03:27 sk.smawsk(a)gmail.com wrote: > Hi, > > Can there be any use of "Pure Virtual Destructor"? > If yes, then in what scenario do we use it? Certainly, you can. It's not tremendously useful other than to force a class to be abstract in the absence of any other pure methods. Note that the pure virtual destructor is still called when the derived class is destroyed so you need to have an implementation of it. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: red floyd on 6 Mar 2007 03:26 sk.smawsk(a)gmail.com wrote: > Hi, > > Can there be any use of "Pure Virtual Destructor"? > If yes, then in what scenario do we use it? > Sure. You have a base class, with no virtual functions, but you don't want it instantiated, ever. struct i_cant_be_instantiated { virtual ~i_cant_be_instantiated() = 0; }; // implementation required, even though it's pure virtual i_cant_be_instantiated::~i_cant_be_instantiated() { } struct ok_to_instantiate { ~ok_to_instantiate() { } }; i_cant_be_instantiated no_way; // error ok_to_instantiate good_example; // OK -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Val on 6 Mar 2007 03:26 { Quoted clc++m banner removed. -mod } This is talked about in detail in Item 14 of "Effective C++" by Meyers. You can't have a pure virtual destructor if the compiler is following the language spec. If you were to derive a concrete class from the abstract one, then create and destroy an object, the base class's destructor would be invoked immediately before the derived destructor. If the base destructor isn't implemented, then this is a problem. The solution is to define an empty implementation for the virtual destructor. Make sure that the destructor is not inlined, however, because its address needs to be saved in the class's vtable. On Mar 6, 5:14 am, sk.sma...(a)gmail.com wrote: > Hi, > > Can there be any use of "Pure Virtual Destructor"? > If yes, then in what scenario do we use it? > > Thanks in advance. > > Warm Regards. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Francis Glassborow on 6 Mar 2007 05:13 In article <1173171845.783991.214940(a)c51g2000cwc.googlegroups.com>, sk.smawsk(a)gmail.com writes >Hi, > >Can there be any use of "Pure Virtual Destructor"? >If yes, then in what scenario do we use it? When you want a base class that is an ABC. Just because a function is declared as a pure virtual does not prohibit a definition being provided. What it does do is to restrict the use of that definition to non-virtual use from within a derived class. In the case of a pure virtual dtor, as long as a definition is provided a derived class dtor can exit through that definition. -- Francis Glassborow ACCU Author of 'You Can Do It!' and "You Can Program in C++" see http://www.spellen.org/youcandoit For project ideas and contributions: http://www.spellen.org/youcandoit/projects [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Dave Steffen on 6 Mar 2007 07:57
"peter koch larsen" <peter.koch.larsen(a)gmail.com> writes: > On 6 Mar., 11:14, sk.sma...(a)gmail.com wrote: > > Hi, > > > > Can there be any use of "Pure Virtual Destructor"? If yes, then > > in what scenario do we use it? > > You can, but I see no reason to have one - except perhaps for one > bizarre situation where the destructor is the main theme of the > class. Keeping it virtual and empty is sufficient. There's good reason to have a pure virtual dtor if the class needs to be abstract, but all the other member functions need to be "impure virtual" (e.g. virtual but not pure virtual) for some reason. Admittedly, I don't have any good examples of why such a design would be useful, but... ---------------------------------------------------------------------- Dave Steffen, Ph.D. Disobey this command! Software Engineer IV - Douglas Hofstadter Numerica Corporation dg(a)steffen a@t numerica d(a)ot us (remove @'s to email me) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |