Solved

How do I destroy an instance of a class?

Posted on 2011-03-07
5
602 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 33

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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