?
Solved

dynamically creating objects only when needed

Posted on 2005-03-26
6
Medium Priority
?
200 Views
Last Modified: 2013-11-17
Hi all,
 you might have come across questions like this one. But I tried a lot in the web but came with no answers for my specific problem.

Ok, here is the question:

I have a scenario where I will create objects only when I need it, I will not even initialize it to any random number of objects during the start of the main() function. All I wanted to do is create one new object of some Class X on call to a function. At the same time, the program should also preserve the values of the objects that are creater earier.

To explain more:
I have got a Customer Class with some fields. I donot want to initialize something around 100 customers at the beginning of the program. I want to create one customer at a time when a customer becomes a member of my company. So, there would be times when I need it, but I donot want to initialize at the start.

[Note: I wanted to do the same for Orders and OrderLines]

I think I have reiterated the same thing again but its jus for the sake of comprehension.

Thank you very much for your efforts to help me.

Cheers,
Raghavan Chockalingam
0
Comment
Question by:raghavan20
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
efn earned 136 total points
ID: 13638458
0
 
LVL 11

Assisted Solution

by:Jase-Coder
Jase-Coder earned 132 total points
ID: 13639907
you could use the TList object with your objects. The TList object will hold all your newly created objects.
Say for example you have a class called TDemo. In your form constructor you could create your TList


MyList = new TList;

I could have the following function

void __fastcall TForm1::Add()
{
  TDemo NewEntry = new TDemo;

  NewEntry->AddName("Jason");
  ..
  ..
  . .
  // once you have populated your object you can add to your TList
  MyList->Add((TDemo *)NewEntry);  
}

thats how to add to the list. To see how many items are in your list you can use MyList->Count - 1;

And to get items from the list you can do:

TDemo *Temp;

Temp = (TDemo *)MyList->Items[1];





0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 13639917
I forgot to mention when deleting from the list you do

MyList->delete( the number in the list)

Doing this deletes the item from the list but your object will be still using memory to solve thisproblem you could do the following:

TDemo *Temp = MyList->Item[ItemToDelete]  //<--- index the items[] like an array
MyList->delete(ItemToDelete);   // take it out of our list
delete Temp; // delete the object that was in the list

btw the reason I am cast when adding to the list and reading from the list is because those functions accept and return void *
0
 
LVL 16

Assisted Solution

by:George Tokas
George Tokas earned 132 total points
ID: 13642774
If it is a class created by you then you should use the new and delete operators..
When you need to use the class to create a new customer or load data for an existing one use in example lets say:
 Customerx = new Customer();//Your class constractor here..
 Do the work you want inside your program and after you finnish with that you release it with delete like:
 delete Customerx...
 I agree with Jase-Coder for the TList to hold the names of the instances of the class you use as referencing the names and the number of the "Customers" you are using at your program..
An advice...
Good design of the class you are about to use is the key to do your life easier on coding.. It will be better to spent a day building a robust class than spent days trying to optimize and debug... The class itself can be add itself and deletes its reference from the list in example again...

gtokas.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

569 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