Question involving classes

B_old
09-22-2002, 10:48 AM
Hello.
I have a question that might be pretty simple for you. I hope you can help.
I have this code and it works.
I can make a particle by writing Particle "particlename" (parameters). What I want to know, is how I can make lots of them using a loop or so. I tried a lots of ways and it did not work. I hope you can help. Thanks



class Particle
{
public:
float left, top, dimension, alpha, radian;

Particle(float object_left, float object_top, float object_dimension, float object_alpha, float object_radian);

};

Particle::Particle(float object_left, float object_top, float object_dimension, float object_alpha, float object_radian)
{
left = object_left;
top = object_top;
dimension = object_dimension;
alpha = object_alpha;
radian = object_radian;
}

Derek Stone
09-22-2002, 11:04 AM
You need to create an array of them. I'm a bit rusty, but here's the jist of it.

Particle *myArray[100];

for (int n=0; n<100; n++) {
myArray[n] = new Particle(<param>, <param>, ...);
}

B_old
09-22-2002, 11:29 AM
Well.
I might work. I get no error then. But when I want to use those Particles it won't work as if I would have created them one by one, if you know what I mean. :(

Derek Stone
09-22-2002, 11:39 AM
Well... C isn't going to magically instantiate them for you. Either do them one by one or use some type of conditional logic.

B_old
09-22-2002, 11:44 AM
Well, I am not asking for magic but this Array-thing should work.
Doing them one by one is out of question as I need up to 5000 of those particles.
If you have a class and want to spwan a lot of objects, what do you usually do I wonder?

noRulez
09-22-2002, 12:49 PM
You need to use Crazed Lunatic's method that he showed you with an array of objects. However, you'll have to come up with a way of giving these objects their respective data. There are a # of possible methods to do this, but it all depends on what you're looking for in your program. It might be that you want to randomly assign values to these objects...but we don't know if that is what you want to do.

edit: Explain what you mean when you say that it won't allow you to use them as if you created them one by one.

JDT
09-22-2002, 01:34 PM
CL's code works if use give the constructor all five args and you will also need to ust the pointer to member operator,


This will make 100 objects and display number 20's left property:


#include "stdafx.h"
#include <iostream>

class Particle
{
public:
float left, top, dimension, alpha, radian;

Particle(float object_left, float object_top,
float object_dimension, float object_alpha, float object_radian);

};

Particle::Particle(float object_left, float object_top,
float object_dimension, float object_alpha,
float object_radian)
{
left = object_left;
top = object_top;
dimension = object_dimension;
alpha = object_alpha;
radian = object_radian;
}


int main(int argc, char* argv[])
{

Particle *myArray[100];

for (int n=0; n<100; n++)
{
myArray[n] = new Particle(1,1,1,1,1);
}

std::cout << myArray[20]->left << std::endl;
return 0;

}

B_old
09-22-2002, 01:59 PM
noRulez:
When I make this:

Particle particle1(1,1,1,1,1);
Particle particle2(1,1,1,1,1);
Particle particle3(1,1,1,1,1);
//...

I can use their left/top/whatever-values.

int test = particle1.left //would be 1

With the array-solution I found no way to use their values I just got errors...
I will try JDT's method now. But is there no way to acces the value like this "particle[n].left" ?
I never used classes before and I have a working particle-emitter now using common arrays, I thought rearranging the emitter as a class might be a good way to learn basics about classes and I think I understand the basic idea about classes now although I don't understand why this is not working

ind count = -1;
int ParticleArray[512];
while (count < 512)
{
count += 1;
Particle ParticleArray[count](1,1,1,1,1);
}

Thank you for the replys! I go for JDT's solution tomorrow but now I am really tired.

JDT
09-22-2002, 02:06 PM
The reason you are getting errors with CL' code is because you are using the dot operator. You need to use the "Pointer To Member" operator instead which is "->"


I have done that for you in the code example I posted.


Good Luck

B_old
09-23-2002, 12:35 AM
OK, but still the dor-operator is working under normal cirmumstances. Today after school I will try your solution.

JDT
09-23-2002, 01:22 AM
The pointer to member notation was just to get CL's code to work.

What you neglected to do in the first place was to provide a default constructor. You only have a five arg constructor. I added a no arg constructor to the class.


See if this is what you are after:


#include "stdafx.h"
#include <iostream>

class Particle
{
public:
float left, top, dimension, alpha, radian;

Particle(float object_left, float object_top,
float object_dimension, float object_alpha, float object_radian);
Particle(); //Default constructor
};

Particle::Particle()
{
left = 1;
top = 1;
dimension = 1;
alpha = 1;
radian = 1;
}

Particle::Particle(float object_left, float object_top,
float object_dimension, float object_alpha,
float object_radian)
{
left = object_left;
top = object_top;
dimension = object_dimension;
alpha = object_alpha;
radian = object_radian;
}


int main(int argc, char* argv[])
{

Particle myArray[100];

std::cout << myArray[20].left << std::endl;
return 0;

}

B_old
09-24-2002, 09:18 AM
I saw no point in writing a no-arg constructor as I intended always to provide those five (in fact more) arguments.
However your code works. I can make an Array, withouth arguments though. And those are very important.
When I tried to give those particles in the array arguments, like this:

Particle myArray[100](500,500,8,1,1);

I got this error:
error C2538: new : cannot specify initializer for arrays

Does this mean that I need a special constructor for particles that come in Arrays?

noRulez
09-24-2002, 06:11 PM
How do you want to assign these things values? If it were random or something, you could assign random #'s to them in the constructor. As it stands now, they are all initialized to 1. Like I said earlier, it just depends on how you want to assign the particle attributes.

Or, at a later point, if you have some way of doing it, you can loop through all your particle objects and assign them values one by one...

B_old
09-25-2002, 06:07 AM
Exactly, noRulez is right! :)
I only have a default Constructor now which I fire one time at the beginning of the programm to get an Array of Objects.
Than I manipulate those opbjects with normal functions whenever I want to.
I guess I was being a little slow as usual...
Thank you all very much for the help!
(especially JTD for all the nice sample-code that eventually brought me on the right way)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum