Trying to understand destructors

I have been reading up on destuctors and what I have been reading says that the destructor is automatically called by the compiler as soon as the scope of the object ends. How does the compiler determine when it is out of scope? I understand variable scope but just don't understand how object scope would work. It seems that a method created by the programmer would destroy the object.

I was also wondering how to destroy and object that gets created but wasn't with in the domain of the params. For example one param is an int that needs to be in between 1 and 10. If user enters an 11 then I want the object to be destroyed.
Last edited on
I understand variable scope but just don't understand how object scope would work.

exactly the same.

If user enters an 11 then I want the object to be destroyed.

you need to check it's between 1 and 10 and not bother creating an object if it's out of your desired range.
Last edited on
For example one param is an int that needs to be in between 1 and 10. If user enters an 11 then I want the object to be destroyed.

Why create it at all? Filter the input before passing the variable to the constructor.

As for your other question, there are a set of rules set by the standard for defining the scope of a variable so there really isn't any guess work involved for the compiler.


Do you care to add a little more to the confusion? For trivial variables, there is no destructor. The data is still there but the compiler invalidates the name, when this quirk is used as an exploit it is referred to as a "read after deletion".
Last edited on
Yes true that makes sense to filter the object when it is created. I was just thinking it might be handy to destruct an object after the wrong inputs have been entered. A way to save me from myself if later down the road when I have another class call this constructor and I forget.

Object Scope - I suppose what gets me when it comes to objects is that I don't see how they would ever leave scope unless destroyed. As with a method variable the variable scope is within that method and discarded after method has been used. I see objects as hanging around until you need to use it again by calling a method.
I think you may be looking at this backwards. A destructor does not destroy an object. A destructor is called when an object is being destroyed.

In the case of statically declare objects (on the stack), consider the following code fragment
1
2
3
4
5
6
7
8
9
10
if (x == true)
{
	int a = 4;
	int b = 6;
	MyClass object1;
	object1.function1(a);
	object1.function2(b);
}
// object1 is no longer available.
...

When the function hits the "if" statement at the beginning, a new object of type MyClass is put on the stack. This means that memory (the size of MyClass) for the object is allocated in the next stack frame and given the name "object1". The integer values a and b are also allocated on the stack.

Some member functions are called on object1, and then the curly brace at the end closes the scope of the "if" statement. At this curly brace, before the"object1 is no longer available" line, a, b and object1 all leave scope, and are destroyed. Nothing happens to the memory allocated on the stack (until the another allocation occurs), but the program cannot access those objects any more. As the object leaves scope, it is destroyed, and the destructor is called. This is especially helpful if the object allocated dynamic memory on the heap (or some other resource) and needs to release it.

When an object is allocated on the heap (with "new"), the life of the object is not dictated by scope of the code. The object lives until "delete" is called. At that point the object is destroyed and the destructor is called.
Last edited on
You're right I was looking at it all wrong. That explanation helped a a lot, thank you!!!
Topic archived. No new replies allowed.