Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I destroy an instance of a class?

Posted on 2011-03-07
5
Medium Priority
?
619 Views
Last Modified: 2013-12-14
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
Comment
Question by:shaolinfunk
5 Comments
 
LVL 8

Accepted Solution

by:
crysallus earned 1600 total points
ID: 35063346
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35063357
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
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 200 total points
ID: 35063382
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
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 200 total points
ID: 35070569
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
 
LVL 1

Author Closing Comment

by:shaolinfunk
ID: 35072958
Thanks crysallus for giving me what I was looking for.  Thanks phoffric and sara for giving me a little extra.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question