Topic: Virtual destructors needed for inherita
Author: zeisel@lnzx16.vai.co.at (Zeisel Helmut)
Date: 1999/05/18 Raw View
In article <37408CE0.D4A3060C@scr.siemens.com>, Michael Greenberg <mgreenberg@scr.siemens.com> writes:
>
>A virtual destructor is required anytime the static type
>of the object differs from the dynamic type. The behavior of the following
>is undefined:
>
>class foo {} ;
>class bar : public foo {} ;
>
>void xxx()
>{
> foo* f = new bar;
> delete f;
>}
>
Maybe I should make my point more clear:
1) The above code is undefined behavior with respect to the standard.
2) AFAIK the above code works fine with almost every current compiler.
3) Was it the intention of the standard committee to make this code
undefined behavior,
or was this just done because it is difficult
to define exactly
when code analogous to the above is safe?
Helmut
--
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: zeisel@lnzx16.vai.co.at (Zeisel Helmut)
Date: 1999/05/18 Raw View
In article <37415322.EB6@eae.com>, Ewert_Ahr._Electronic_GmbH@t-online.de (Gernot) writes:
>
>Zeisel Helmut wrote:
>> In particular, I was experimenting with a class design
>> where "copy-on-write" with proxy classes is replaced by
>> a const base class and a derived class that adds the mutable part,
>> say
>>
>> class A
>> {
>> friend class mutableA;
>> public: // only constructor and const member functions...
>> private: // data members
>> };
>>
>> class mutableA: public A
>> {
>> public:
>> // adds non-const member functions
>> };
>>
>> The constructor A(const A&) just increases a reference count
>> while A(const mutableA&) makes a deep copy and so on.
>>
>
>I have done exactly the same thing (BTW, it proves to be extremly
>handy!) and found no problems. If you delete an object of your class
>"mutableA" with a pointer to the base class "A", the non-virtual base
>class destructor is called, which is totally sufficient for this
>application. (No need to remind me that this is "non-standard", please!)
>
Fine, so some other person also finds this approach useful.
Maybe we can convince people from the standard committee to relax
5.3.5-3 in a future revision
to make deleting through a base class pointer defined behaviour,
when the derived class is "delete compatible",
where "delete compatible" means that no nonstatic data members but
only nonvirtual member functions (and static data members)
are added in the derived class.
I am not sure whether it would also be feasible
to add virtual functions and remain "delete compatible",
but when there are already some virtual functions,
there are no real arguments against a virtual destructor.
Helmut
--
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]