Solved

How do I destroy an instance of a class?

Posted on 2011-03-07
5
598 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 400 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 50 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 32

Assisted Solution

by:sarabande
sarabande earned 50 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now