important question

how its possible to add a destructor for the following Foo class?

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
class Foo {
public:
 Foo()
 { std::cout << "Foo constructor 1 called" <<
std::endl; }
int main( )
{
 Foo foo_1;
 return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

class Foo
{
    public:
        Foo()    //constructor
        {
            std::cout << "\nFoo constructor 1 called\n";
        }
        ~Foo()    //destructor
        {
            std::cout << "Foo destructor 1 called\n";
        }
};

int main()
{
    Foo foo;
    foo.~Foo();
    
    return 0;
}


Last edited on
its an example so its ok here, but generally you don't want to explicitly call a dtor that way. scope it and let it self-destruct to test it. a simple way:

void testdtor()
{
Foo f;
}

main()
{
testdtor();
}

If you report everyone who tells you a truthful answer, you will soon not have any help... the report function is not a dislike button.
1
2
3
4
5
6
7
int main()
{
    Foo foo;
    foo.~Foo();
    
    return 0;
}
Don't do this.

The lifetime of the object ends when control enters its (non-trivial) destructor.
Explicitly calling the destructor does not suppress the implicit call when the object goes out of scope. Consequentially, the implicit call to the destructor is made on "not-an-object" and so the behavior is undefined.
https://en.cppreference.com/w/cpp/language/lifetime

There are rare cases where explicit calls to the destructor are correct and even required. A beginner shouldn't bother with them.
Last edited on
@mbozzi

I know you're right, I was merely giving an example to show how to define the constructor in a class and showing that it did indeed work when you call it. My bad, should have done that instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

class Foo
{
    public:
        Foo()    //constructor
        {
            std::cout << "\nFoo constructor 1 called\n";
        }
        ~Foo()    //destructor
        {
            std::cout << "Foo destructor 1 called\n";
        }
};

void testFunction()
{
    Foo f;
    
    return;
}

int main()
{
    testFunction();
    
    return 0;
}


Foo constructor 1 called
Foo destructor 1 called
Last edited on
There are rare cases where explicit calls to the destructor are correct and even required.

When would explicitly calling the dtor be useful?
The most common use-case is when we want to separate acquiring and releasing memory from creation and destruction of objects to be placed in that memory.

For instance, when we do a pop_back() on a vector, the object at the back of the sequence must be destroyed; but the reserved memory remains allocated. Every allocator aware container needs to separate these two concerns.
A vector is a class of a class? That makes sense. Thanks a lot for the example. So when you resize a vector you're really instantiating more objects inside the vector class. What about reserve? I need to look at how vectors are implemented.

If a vector is not a class of a class then the individual elements wouldn't have ctors and dtors.. only the entire vector would..

And back to what mbozzi said. He said there can be uses for explicit calls to the destructor. Are there? Or did he perhaps mean what you said - explicit calls to releasing memory.
Last edited on
See the discussion here for a kind of pessimized example of an explicit destructor call:
http://www.cplusplus.com/forum/beginner/210637/
(Where the issue of exception safety is ignored in the interest of clarity.)
Last edited on
Topic archived. No new replies allowed.