Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a ton of objects at once

Status
Not open for further replies.

minoad

Programmer
Mar 28, 2001
138
US
I have a class which in its constructor will create 60 instances of another class. It would be nice if i could simply loop through the creation of all of these classes, however i dont know how to dynamically create the names. Mayby an array of objects? is this possible in C++. Something similar to the below pseudo code:

array[60]

for each item in array:
new object
array::name = "a string"
array::value = an integer value

This may not be very clear, but does anyone see what i am trying to do here.

Any Help would be appreciated.

Micah A. Norman
 
How about:

CMyClass * pmyClass = new MyClass[60];

Then

pmyClass[60].value = 3;

Cheers,

Vincent
 
What's a problem? Suppose (for simplicity) you have the class:
Code:
class Item
{
public: // same as struct Item (for example only)
  Item():value(0) {}
  Item(const string& s, int ival = 0):value(ival) {}
  string name; // No need in special dtor in that case
  int value;
};
Then
Code:
Item array[60];
for (int i = 0; i < 60; ++i)
   array[i].value = i;
// I don't know where is new names source. For example:
// cin >> array[i].name; // not safe source, but...

You also may use std::vector<Item>:
Code:
vector<Item> v(60);
// Then as in the case above, or
vector<Item> array;
Item it;
for (int i = 0; i < 60; ++i)
{
  it.name  = ...;
  it.value = ...;
  array.push_back(it);
}
You may create this array on the fly:
Code:
Item* items;
items = new Item[60];
for (int i = 0; i < 60; ++i)
{
   items[i].name  = ...;
   items[i].value = ...;
}
// Don't forget to deallocate:
delete [] items;
 
Do you want to do something like this?

Code:
// Initial values for a Foo
struct FooDesc
{
    const char *name;
    int val;
};


// List of initial values for a FooList to start with
static FooDesc descriptions[] = {
    {"one",    1},
    {"two",    2},
    {"three",  3},
    // ...
    {"twelve", 12},
    {0, 0}           // Signifies the end
};


// FooList constructor:
// Walks through the list and creates a list of Foos
// based on the descriptions.
FooList::FooList()
{
    for (FooDesc *p = descriptions; p->name; ++p)
        array.push_back (Foo (p->name, p->value));
}


Unless there's a pattern in the objects' values that can be expressed as an algorithm, there's obviously no way to automatically generate them. You'll have to give a list of some or all the data you need, like in the above example.

The best place for that list is either as a global but [tt]static[/tt] variable that lives in the same module as the constructor (as above), or as a [tt]private[/tt] [tt]static[/tt] variable of the [tt]FooList[/tt] class.
 
Actually, I guess [tt]static[/tt] globals are deprecated in C++. Equivalently, you could put it in an anonymous namespace.
Code:
namespace
{
    // whatever
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top