shaolinfunk
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.PerformCalculatio ns. 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.PerformCalculatio ns(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?
So:
Class Instance
Then, in Instance I run a bunch of calculations using data pertaining to 1 student in my class, by running Instance.PerformCalculatio
Class Instance
For (x=0; x < numStudents; x++)
{
Instance.PerformCalculatio
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks crysallus for giving me what I was looking for. Thanks phoffric and sara for giving me a little extra.
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).