Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I destroy an instance of a class?

Posted on 2011-03-07
5
Medium Priority
?
609 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

730 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