Destroying any and all objects

How would I 'destroy' (destructor or other), or reset the data members of any given amount of objects? Likely, I would be using a single global/static function to perform the clear in one swoop.

If the above isn't enough, the details include a class with member components that deal with text manipulation on the screen: row/column location, color, center positioning, etc. Calling a function that clears any number of these objects and resets the screen is what I am trying to achieve. The user would afterwards have to declare new objects.
Can't you have a reset function?
If you have many objects inside a container you just have to iterate over all of the objects and call the reset function.
Object iteration, as you suggested, is a great way to put it. I guess I am asking the best way to do this and am considering STL list to pull this off.

Also, is calling the destructor explicitly through a function good practice?

Thanks for your reply.
Also, is calling the destructor explicitly through a function good practice?

No, that's not allowed for objects that haven't been created using placement new.
If your objects are in a STL container, you can just call clear() to destroy them all.
What container would you suggest? I still would like for the client to be able to create objects when needed via ClassName objectName. I guess I'm a little lost on how to keep track of everything and execute that clear() when needed.

Maybe there is another way to do this that keeps the simplicity for the user.
When you use the 'new' keyword, you can create an object in dynamic memory. You must use a pointer to do this:

MyClass * myDynamicObject = new MyClass;

Now, when you want to get rid of the object and create a new one, you can do this:

delete myDynamicObject;
myDynamicObject = new MyClass;

This is how you 'destroy' objects, and the destructor is called when it happens. Note that the destructor isn't a 'destroyer,' it is the final function called before the program deletes the dynamic object. If YOU call the destructor all by yourself, the object will not be destroyed.

Now, using this newfound knowledge, you can research pointers and figure out how to make an array, then iterate through it deleting every object you need to delete.

Yes, this all isn't new to me. I just haven't needed to use objects in this way before and was curious on the preferred method of containing them.

Thanks for the tidbit on destructors.
I still would like for the client to be able to create objects when needed via ClassName objectName.

Then the objects have to add and remove themselves from that list. You can store either a copy of the objects or a pointer to them, depending on what you're trying to accomplish. When you store a pointer, you cannot destroy them and you have to resort to calling a reset function on each.

What container would you suggest?

list. It allows adding and removing elements in constant time (if you keep the iterator insert returns).
Last edited on
Great! Thanks for the help everybody.
Topic archived. No new replies allowed.