Link to home
Start Free TrialLog in
Avatar of shaolinfunk
shaolinfunkFlag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of crysallus
crysallus
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

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).
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shaolinfunk

ASKER

Thanks crysallus for giving me what I was looking for.  Thanks phoffric and sara for giving me a little extra.