0

I would want to know if it is useful to put a destructor in constant? E.g.:

class example
{
    public : 

    example(int params); 
    const ~example(); //Here
}
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Adam Brevet
  • 37
  • 1
  • 7

3 Answers3

2

From a conceptual standpoint, this does not make any sense. const functions have no side-effects. Thus, they need to return something. As the destructor does not return anything, this function would be useless.

(Similar to a const void function by the way)

As noted in the comments, you could still modify mutable data in a const.

mrks
  • 8,033
  • 1
  • 33
  • 62
  • 1
    except in the presence of `mutable` data... – melak47 Oct 23 '15 at 11:22
  • 4
    It is not true that `const` functions have no side effects. They may produce output (e.g. write to a file, `std::cout`, etc). They may access global variables and change them. – Peter Oct 23 '15 at 11:27
  • Good point - I didn't think of that. Does this make sense to be added to the answer? I feel like my original understanding was not completely correct. – mrks Oct 23 '15 at 11:38
  • Thank you, I did not know because I am novice – Adam Brevet Oct 23 '15 at 13:38
1

It's not useful, because it's not possible:

error: qualifiers are not allowed on destructor declaration

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0

It is not useful, and would typically trigger a warning from the compiler - usually to the effect of the qualifier being ignored.

A const qualifier on the return from a function is mostly only useful for functions that return a pointer or reference (e.g. to indicate that the returned reference is to an object that should not logically be changed). A destructor does not return anything so there is nothing for the const to qualify.

Making the destructor itself const (i.e. ~example() const) will be rejected by the compiler. That is appropriate since a destructor generally changes state of the object (causes it to no longer logically exist)

Peter
  • 35,646
  • 4
  • 32
  • 74