Topic: private/protected destructors


Author: d96-mst@nada.kth.se (Mikael St ldal)
Date: 1997/01/20
Raw View
In article <2.2.32.19970114001612.00313500@central.beasys.com>,
David R Tribble <david.tribble@central.beasys.com> wrote:
>>   (3) "final" classes cannot be extended (derived).
>>
>>... One important reason for (3) is that it
>>prevents problems with destructors.  If one can prevent inheritance
>>from class A, then A can have a non-virtual destructor.  ...
>
>Stroustrup discusses this in the D&E:
>
>    A private destructor also prevents derivation. ...

But that also prevents normal use of the class, not very good. Isn't a
class with private destructor useless?

A similar technique, protected destructor, can be used as an
alternative (instead of pure virtual functions) way of making an
abstract base class. Does that technique has any problems?
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: phalpern@truffle.ma.ultranet.com (Pablo Halpern)
Date: 1997/01/22
Raw View
d96-mst@nada.kth.se (Mikael St=E5ldal) wrote:

>In article <2.2.32.19970114001612.00313500@central.beasys.com>,
>David R Tribble <david.tribble@central.beasys.com> wrote:
>>>   (3) "final" classes cannot be extended (derived).
>>>
>>>... One important reason for (3) is that it
>>>prevents problems with destructors.  If one can prevent inheritance
>>>from class A, then A can have a non-virtual destructor.  ...
>>
>>Stroustrup discusses this in the D&E:
>>
>>    A private destructor also prevents derivation. ...
>
>But that also prevents normal use of the class, not very good. Isn't a
>class with private destructor useless?

Not necessarily. A class with a private destructor can be destroyed by
friends of the class or by members of the class. For example, if the
class provides factory functions and self-destruct functions, then
it is perfectly useful and has the extra property that all instances are
guaranteed to be on the heap:

  class myclass
  {
    public:
      static myclass* make() { return new myclass; }
      void destroy() const { delete this; }
    private:
      // Constructors and destructor are private:
      myclass();
      myclass(const myclass&);
      ~myclass();
  };

>
>A similar technique, protected destructor, can be used as an
>alternative (instead of pure virtual functions) way of making an
>abstract base class. Does that technique has any problems?

Protected destructors work fine to make a class abstract and also to
prevent destruction of an object of that class except through friends or
members. However, it does not prevent derivation.

-------------------------------------------------------------
Pablo Halpern                   phalpern@truffle.ultranet.com

I am self-employed. Therefore, my opinions *do* represent=20
those of my employer.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]