Topic: Destructors


Author: dag@control.lth.se (Dag Bruck)
Date: Mon, 17 May 1993 13:48:23 GMT
Raw View
In <comp.std.c++> g2devi@cdf.toronto.edu (Deviasse Robert N.) writes:
>
>Given the following code:
>      class C {};
>      C c;
>      int x(2);
>      c.C::~C();               // legal?    (1)
>      x.int::~int();           // legal?    (2)
>
>Are (1) and (2) legal?

It is legal; see ARM at the end of Section 12.4.


   -- Dag




Author: g2devi@cdf.toronto.edu (Deviasse Robert N.)
Date: Fri, 14 May 1993 19:13:22 GMT
Raw View
Given the following code:
      class C {};
      C c;
      int x(2);
      c.C::~C();               // legal?    (1)
      x.int::~int();           // legal?    (2)

Are (1) and (2) legal? Since default constructors are provided for all types,
shouldn't default destructors also be provided, even if they are just NOPs?

The reason I'm asking this is I have a few generic templates that make
explicit calls to the destructor of the type argument of the template. The
solution I had to settle with is to define a destructor template and *for
each* class that did not have a destructor, I had to specialize the template
to be a NOP.

template<class T>
   struct destructor{                   // Default behaviour
      destructor(T& t) { t.T::~T(); }   // Call any destructor available
   }

struct destructor<C>{       // specialization to define destructor for class C
   destructor(C&) {}        // NOP
};


This works, and true, any classes that do not have a destructor will give a
compile time error so this method is "safe", but it's clearly a kludge to fix
a problem I don't think should exist.

Take care
    Robert
--
/----------------------------------+------------------------------------------\
| Robert N. Deviasse               |"If we have to re-invent the wheel,       |
| EMAIL: g2devi@cdf.utoronto.ca    |  can we at least make it round this time"|
+----------------------------------+------------------------------------------/




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sat, 15 May 1993 16:43:05 GMT
Raw View
In article <1993May14.191322.26455@cdf.toronto.edu> g2devi@cdf.toronto.edu (Deviasse Robert N.) writes:
>
>Given the following code:
>      class C {};
>      C c;
>      int x(2);
>      c.C::~C();               // legal?    (1)
>      x.int::~int();           // legal?    (2)
>
>Are (1) and (2) legal? Since default constructors are provided for all types,
>shouldn't default destructors also be provided, even if they are just NOPs?

 Sure? Are you sure default constructors are provided for
all type (that have no other constructors specified).

 Dont see how you can be sure when the ARM contradicts itself
on this issue (see aggregates .,. things with no constructors ..)

 I think you *should* be correct though (with the caveat about
no other explict constructors). Tom Pennello wrote a paper on
this and called the default constructors that got 'generated'
but never did anything 'trivial constuctors'.

 I guess we would have destructors for all classes then.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA