You don't call a destructor like that (well, you can, but it hasn't been done at all).
For automatic variables such as b , the destructor will be called at some point when the variable goes out of scope. You never need to explicitly call the destructor.
For objects allocated on the heap with new , the destructor will be called after you delete . In this case, you also do not call the destructor explicitly.
C ++ 03 at 12.4 Destructors :
Destructors are called implicitly:
- for a constructed object with a static storage duration (3.7.1) at the end of the program;
- for a constructed object with automatic storage time (3.7.2), when the block in which the object is created is called;
- for the created temporary object, when the lifetime of the temporary object ends;
- for the constructed object selected by the new expression, using the delete expression;
- in several situations due to exception handling.
Destructors can also be explicitly called.
Note: calls to destructors are clearly needed. One use of such calls is for objects placed at specific addresses using a new expression with the possibility of placement. Such use of explicit placement and destruction of objects may be required to cope with the allocated hardware resources and to write memory controls.
You especially do not do what you are trying to do, since the destructor will be called twice, once explicitly, and you will also once implicitly when b goes beyond. From the same section of the standard:
As soon as the destructor is called for the object, the object no longer exists; undefined behavior if the destructor is called for an object whose lifetime has expired. Example: if the destructor for an automatic object is explicitly called, and then the block is left in a way that usually causes implicit destruction of the object, the behavior is undefined.
This text remains unchanged in the last C ++ 11 project that I have (n3225, November 2010), and it is unlikely that it would essentially change between this and the statement in August 2011.
source share