dynamically creating objects only when needed

Posted on 2005-03-26
Medium Priority
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.

Raghavan Chockalingam
Question by:raghavan20
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
  • 2
LVL 15

Accepted Solution

efn earned 136 total points
ID: 13638458
LVL 11

Assisted Solution

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;

  . .
  // 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];

LVL 11

Expert Comment

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 *
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...


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

752 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