Home c++ Why C++ allows you to call the destructor constant object manually?

Why C++ allows you to call the destructor constant object manually?

Author

Date

Category

Please tell me what is the reason that the language allows you to call the destructor for the constant term (manually)? After changing the state of the object’s destructor. Moreover, not only the state of the deleted object, but also the state of the object owner.

class Composition
{
  Public:
  // ...
  const Object object;
};
// ...
Composition composition;
composition.object ~ Object ().;

Answer 1, Authority 100%

Standard:

A destructor is used to destroy objects of its class type. A.
destructor takes no parameters, and no return type can be specified
for it (not even void). The address of a destructor shall not be
taken. A destructor shall not be static. A destructor can be invoked
for a const , volatile or const volatile object. A destructor shall not
be declared const , volatile or const volatile (9.3.2). const and
volatile semantics (7.1.6.1) are not applied on an object under
destruction. They stop being in effect when the destructor for the
most derived object (1.8) starts. A destructor shall not be declared
with a ref-qualifier.

Translation:

The destructor is used to destroy objects of its type.
Destructor takes no parameters, and it is impossible to specify the type for it
return value (even void). Address destructor is not taken.
Destructor should not be static. Destructor can be called for
property type const , volatile or const volatile . Destructor should not
be declared as const , volatile or const volatile (9.3.2).
Semantics const and volatile (7.1.6.1) does not apply to the damaging
object. They cease when the destructor is run for
derivative of the object (1.8). The destructor should be declared
with ref-qualifier.

The logic is simple, the object has life come to an end. Even the constant.

// g ++ -std = C++ 11 -Wall constdestr2.cpp
#include & lt; new & gt;
class C {
public:
 C (int x): i {x} {}
 C (C const & amp; & amp; c): i {std :: move (c.i)} {}
 ~ C () {}
private:
 int i;
};
INT MAIN () {
 // memory somewhere far
 char mem [sizeof (C)];
 // to manually create a constant object
 C const * c = new (mem) C {7};
 // allocate a new memory
 char mem2 [sizeof (C) * 2];
 // const object transfer
 C const * c2 = new (mem2) C {std :: move (* c)};
 // constant manual removal
 c - & gt; ~ C ();
 // now constants vector elsewhere
 // constant manual removal
 c2 - & gt; ~ C ();
}

Here is an example of the constant increase in the size of the array of objects. Where each object needs to transfer: Transfer constructor and destructor call

.


Answer 2, authority 100%

It :

The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.

Note that calling a destructor directly for an ordinary object, such as a local variable, invokes undefined behavior when the destructor is called again, at the end of scope.


The problem destructor – free resources that it can capture in the course of life

.

Note that the destructor call directly for a simple object, such as a local variable, resulting in undefined behavior when the destructor is called when leaving the scope.

Destructor – a special class method must be called once. He does not change the state of the object. He destroys it. Therefore, the semantics of const or volatile (as correctly noted AlexGlebe) does not apply to him.

The example will call the destructor twice (directly and when out of scope) and UB as the result.

P.S. If a language allows dereferencing a null pointer, this does not mean that it can be done so.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions