English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have defined a class,
As part of the main program, the number of instances of the class I want to create is entered by the user, how can I create all these instances of the class, each with a different name e.g. K1, K2, K3 etc.

What I have at the moment is, nocharge is the number of instances I want to create, and Cpointcharge is the name of the class, but this doesn't work. I need some help:
for (int i = 0; i <= nocharge; ++i)
{
Cpointcharge K[i];
}

thanks (I've only been programming for a couple of weeks so its probably something fairly simple).

2007-01-30 07:56:30 · 3 answers · asked by Om 5 in Computers & Internet Programming & Design

3 answers

Hi,

That code above will not work since you sort of declaring an array in a for loop. You should dynamically allocate your array based on user's input. Like this:

pChargeArray = new pChargeArray[nocharge];

This will invoke the no argument constructor of that class.
Remeber to delete this object using the bracket notation.

2007-01-30 08:54:54 · answer #1 · answered by JackBauer 2 · 0 0

The way you are trying to do it requires the new and delete operator.

Cpointcharge* K[i] = new Cpoincharge();
Then, somewhere else in you code...
for (int i = nocharge; i > 0; i--)
{
delete K[i];
}

Think of an object as being no different than the declaration of an integer. An integer can be thought of as an object and I believe it is in the Java language. If fact you could create a class with just an integer.

Try playing with something basic like this to get a handle on using objects.

class MyInteger
{
private:
int x;
//Copy Constructor
public:
MyInteger::MyInteger(int NewX)
{
x = NewX;
}
public:
//Can't remember how, but add an override for the equal operator.
operator = (int NewX){ x = NewX; }
void SetX(int newX){ x = NewX; }
}

#define MAX_SIZE 20
MyInteger K[MAX_SIZE];
int count = 20;
for (int i = 0; i <= count; ++i)
{
K[i] = i;
}

You will have to use copy constructors and override of the equal operator to copy objects.
You could do an assign instead and do not have to use copy constructors or override the equal sign, but I don't know what you are trying to do.

//You could do it this way instead.
for (int i = 0; i <= count; ++i)
{
K[i].SetX(i);
}

"thanks (I've only been programming for a couple of weeks so its probably something fairly simple)."
Nope. Using pointers and remembering to delete objects is the toughest part of programming.

2007-01-30 16:24:06 · answer #2 · answered by Anonymous · 0 0

You could create a linked-list of objects that you can store your new classes in. In this case they're anonymous classes (e.g. no name), but you can reference each one individually. If you really need to name each one you could use a Hashtable instead.

(i haven't done c++ in a while, so this may not by syntactically correct, but here's my solution:)

List list = new List(); //create an empty linked list

for (int i = 0; i <= nocharge; ++i)
{
list.add(new Cpointcharge());
}

Then you can reference each one like:

for (int i = 0; i <= nocharge; ++i)
{
printf("%s", list[i].toString());
}

hope this helps...

2007-01-30 16:14:57 · answer #3 · answered by fixedinseattle 4 · 0 0

fedest.com, questions and answers