Hi all!

I'm having some trouble with constructors and destructors. I've been told that a constructor creates an object and a destructor destroys it.

The problem is that in my code I try to destroy straight2, but I can still print out it's length as if it wasn't destroyed at all. After that it gets destroyed again.

Can somebody please explain what I'm doing wrong?

#include <iostream>

using namespace std;

class Line
{
    public:
        int length;
        Line(); // Constructor declaration
        ~Line(); // Destructor declaration
        int getLength(void);
};

Line::Line()
{
    cout << "Line constructed" << endl;
    length = 10;
}

int Line::getLength(void)
{
    return length;
}

Line::~Line()
{
    cout << "Line destructed" << endl;
}

int main()
{
    Line straight1;
    Line straight2;

    cout << "First line length: " << straight1.getLength() << endl;

    straight2.~Line();

    cout << "Second line length: " << straight2.getLength() << endl;

return 0;
}

Bad code. Your Line objects are on the stack. Even though you call the destructor, until something else overwrites that memory, the length value will still be in memory, and unchanged. Try setting length to -1 in the destructor, and then see what is printed.

Also, read up on the scoping of automatic variables, which is what straight1 and straight2 are.

I see that the destructor of an automatic variable gets called at the end of a program block.

I still face the same problem:
I was expecting somekind of error in the 39th line of code, since straight2 shouldn't exist anymore. So I'm guessing the destructor doesn't really get rid of the object?

And please explain what you meant by 'on the stack'.

A destructor is called (automatically) when an object is destroyed. It does not itself destroy the object. An object is destroyed when it goes out of scope or when delete is used on it (in case of objects created using new).

You should never call a destructor manually unless you're doing advanced manual memory management (like defining your own allocator using placement new).

The purpose of the d'tor is not to free the object itself; that is done by delete, which should only be applied to dynamically allocated memory (that is, values allocated using new) in any case. Also, freeing the object does not overwrite the object; the memory itself is is still unchanged, at least for the time being, and in order to ensure that a pointer variable no longer points at that now-invalid memory location, you need to assign a new value (such as 0) to it.

The purpose of the destructor is to clean up the elements of the objects that are not automatically released when the object itself is freed: deleteing any dynamically allocated instance values, closing files, releasing semaphores, and the like.

As sepp2k said, the d'tor should almost never be called directly. However, if a class has a d'tor, it is called automatically when an object of the class is freed, whether by means of delete or when a local goes out of scope.

Oh I see now.

Thanks for clearing things up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.