• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

How do I destroy an instance of a class?

Let's say I have created class called Class.  Then, I create an instance of Class called Instance.

So:

Class Instance

Then, in Instance I run a bunch of calculations using data pertaining to 1 student in my class, by running Instance.PerformCalculations.  Now imagine that I have 500 students.  So I created a For loop such that it iterates through the calculations 500 times.  

Class Instance

For (x=0; x < numStudents; x++)
{
           Instance.PerformCalculations(x);  //x identifies each unique student
}

Rather than resetting all of the variables in Instance to 0 to calculate the data for a new incoming student, I just want to DESTROY the instance and make a new one.  How do I destroy "Instance"?

Then, after answering my question, is this the best way to do things?
0
shaolinfunk
Asked:
shaolinfunk
3 Solutions
 
crysallusCommented:
You could simply create the Instance within the for loop, so it gets created anew at the start of each iteration.

for (x=0; x < numStudents; x++)
{
           Class Instance;
           Instance.PerformCalculations(x);  //x identifies each unique student
}

Open in new window


Though I don't see how re-creating the class every time is going to be any more efficient than resetting its variables. So no, I don't think this would be the best way to do it. I'd just create a method within Class called Reset or Init, and just call that instead.
0
 
phoffricCommented:
If you created Instance by using new, then to delete the object,
   delete Instance;
will delete this Instance object. If  you have a destructor defined in your class,
     ~Class() { ... }
then this destructor will be called.

If you have a reset() function to zero out your calculations, that may be faster than the new/delete operations. The reset() function has the added benefit of less chance of memory fragmention (which isn't usually a problem these days on workstations).
0
 
phoffricCommented:
If you do not use new (and so you do not use delete), then the instance is automatically destroyed when the Instance object goes out of scope.

For example,
    int foo() {
        Class Instance;
        ... // other processing
        return some_int;
    }

When foo() returns, Instance is no longer in scope by the caller. In that case the Instance object is automatically destroyed, since Instance is an auto-variable object on the stact of foo(), and after the return, the foo() stack no longer exists. If you have a destructor, then as foo() is returning, the Class destructor for Instance will be called automatically.
0
 
sarabandeCommented:
if the PerformCalculations would set any results to the Instance you probably would like to keep the Instance and not to destroy the results by next calculation.

to solve that you would store instances in a vector in the loop. it doesn't matter whether you created the Instance outside or inside of the loop cause std::vector wouldn't store the original instance but a copy.

using the code crystallus has posted it would turn to

std::vector<Class> instances;
for (x=0; x < numStudents; x++)
{
           Class Instance;
           Instance.PerformCalculations(x);  //x identifies each unique student
           instances.push_back(Instance);
} 

Open in new window


Sara
0
 
shaolinfunkAuthor Commented:
Thanks crysallus for giving me what I was looking for.  Thanks phoffric and sara for giving me a little extra.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now