declare a class pointer

M

Mike

Hi
how do i declare a class pointer?

int * drops[] = new droplet(int, int, int); // WRONG

--------- // later

class droplet
{
public:
int trail[16];//information on all characters
int speed;//speed, 1, 2 or 3.
int X, Y;
droplet(int x, int y, int spd);
~droplet();
....

--------later i call it alike:

for(int dDrops=0;dDrops<num;dDrops++)
{
drops[dDrops] = new droplet(...
 
S

SG

Hi
how do i declare a class pointer?

like any other pointer:

myclass* pointer_variable;
int * drops[] = new droplet(int, int, int);  // WRONG

Yes, there's a lot wrong with it. In fact, there's so much wrong with
it, that I don't know what you were trying to do. (hint hint)
--------- // later

class droplet
{
public:
        int trail[16];//information on all characters
        int speed;//speed, 1, 2 or 3.
        int X, Y;
    droplet(int x, int y, int spd);
    ~droplet();
...

If you don't need a custom destructor, just don't declare/define it.
--------later i call it alike:

   for(int dDrops=0;dDrops<num;dDrops++)
   {
        drops[dDrops] = new droplet(...

So, you want "drops" to be some kind of "array" which stores pointers
to droplet objects. Seeing that you have trouble expressing C++
programs chances are that this isn't a good design approach. So, you
might want to consider various approaches with their varying
properties:


1. "Fixed array" of droplet-pointers

droplet* drops[num]; // num needs to be a compile-time constant
for (int i=0; i<num; ++i)
drops = new droplet(1,2,3);
...
for (int i=0; i<num; ++i)
delete drops;


2. Dynamically-allocated array of droplet pointers:

droplet** drops = new droplet*[num];
for (int i=0; i<num; ++i)
drops = new droplet(1,2,3);
...
for (int i=0; i<num; ++i)
delete drops;
delete[] drops;


3. "Dynamic array" of droplet pointers

std::vector<droplet*> drops;
for (int i=0; i<num; ++i)
drops.push_back(new droplet(1,2,3));
...
for (int i=0; i<num; ++i)
delete drops.at(i);


4. "Dynamic array" of droplets

std::vector<droplet> drops;
for (int i=0; i<num; ++i)
drops.push_back(droplet(1,2,3));


I included the necessary "clean up" code after the "...". In the last
case there is no need to do anything special in terms of cleaning up.
The std::vector manages its own droplet copies automatically. Note
that I didn't use any new-expression in the last case. You should try
to delegate things like dynamic memory management to "smart" objects
like std::vector. Note also, that in the 3rd case the vector stores
pointers. In the vector's destructor the pointers are correctly
destroyed but not those objects they point to which is why there's a
loop over all pointers after "..." to delete those objects. Another
thing I'd like to mention is that only approach 4 is exception-safe in
the sense that if an exception is thrown somewhere during the droplet
particle creation (unlikely) or during the "..." you'll get memory
leaks.

You are likely to get better answers if you describe what you are
trying to do (not how).

Cheers,
SG
 
M

Mike

Many thanks. Now i try the std vector.

class droplet
{
public:
int trail[16];
int speed;
int X, Y;
droplet(int x, int y, int spd);
~droplet();
....
}

vector<droplet> drops[200];

-------in main()

for(int dDrops=0;dDrops<num;dDrops++)
{
drops[dDrops].push_back(droplet(rand()%PanelLeftrain + 20,rand()%
(PanelToprain - 30),rand()%2+1));
// all ok so far

for(int a=0;a<drops[dDrops]->speed*5+2;a++) //ERROR
if(drops[dDrops]->Y-dDrops>0||drops[dDrops]->Y-
dDrops<PanelTop-2) //ERROR
{

How should I access speed and X/Y as declared in droplet class?
Many thanks again


Michael
 
M

Mike

PS: can i use drops for functions like fadeout etc.? Created at init
and deleted at program exit.
I want to move and fadeout drops while creating new ones aside in the
main loop.
It's for a rainsimulation on a (car whatever) window. Thanks again
 
Ö

Öö Tiib

Many thanks. Now i try the std vector.

class droplet
{
public:
        int trail[16];
        int speed;
        int X, Y;
    droplet(int x, int y, int spd);
    ~droplet();
...

}

vector<droplet> drops[200];

-------in main()

           for(int dDrops=0;dDrops<num;dDrops++)
           {
     drops[dDrops].push_back(droplet(rand()%PanelLeftrain + 20,rand()%
(PanelToprain - 30),rand()%2+1));
        // all ok so far

               for(int a=0;a<drops[dDrops]->speed*5+2;a++) //ERROR

Does not your compiler tell that you perhaps used "->" instead of "."?
Several do.
                   if(drops[dDrops]->Y-dDrops>0||drops[dDrops]->Y-
dDrops<PanelTop-2) //ERROR

Same here.
                   {

How should I access speed and X/Y as declared in droplet class?

Using syntax like "drops[dDrops].X".
 
M

Mike

Wheter I use -> or . i get an error

So to get back to my first trial how should my initial declaration
look like?

droplet drops[200];

results in error.
also

droplet drops(int, int, int);

Thanks
 
Ö

Öö Tiib

Wheter I use -> or . i get an error

So to get back to my first trial how should my initial declaration
look like?

droplet drops[200];

results in error.
also

droplet drops(int, int, int);

I have found it to be helpful to read what does the error message say.
Compiler usually does indicate the things that it does not like in
error message. Also it usually does point at such things with accuracy
of single character. Sometimes the messages are cryptic when using
very complex templates, but this is not the case with vector
containing objects of some simple class.
 
S

SG

Many thanks. Now i try the std vector.

class droplet
{
public:
        int trail[16];
        int speed;
        int X, Y;
    droplet(int x, int y, int spd);
    ~droplet();
...
}

If droplet::~droplet doesn't do anything you *could* just remove your
custom destructor. Just saying.
vector<droplet> drops[200];

This defines 'drops' to be an array of vectors of droplets. Is this
really what you want?
-------in main()

 for(int dDrops=0;dDrops<num;dDrops++)
 {
   drops[dDrops].push_back(
droplet(rand()%PanelLeftrain + 20,
rand()%(PanelToprain - 30),
rand()%2+1) );
    // all ok so far

    for(int a=0;a<drops[dDrops]->speed*5+2;a++) //ERROR
      if(drops[dDrops]->Y-dDrops>0
|| drops[dDrops]->Y-dDrops<PanelTop-2) //ERROR
      {

How should I access speed and X/Y as declared in droplet class?

If 'drops' is of type vector<droplet>[200] (array of vectors)
then 'drops[dDrops]' is of type vector<droplet> (vector). But
std::vector doesn't have an overloaded -> operator which is why
'drops[dDrops]->Y' is ill-formed.

You can access the first element in a vector using front like this:

drops[dDrops].front().Y

But I have the feeling that you don't actually want an array of 200
vectors that store only one droplet each.

vector<droplet> drops[200]; // array of 200 vectors of droplets
vector<droplet> drops(200); // vector of 200 droplets

In the first line [200] is part of the type of drops. In the second
line (200) is the syntax for "calling" the vector's constructor and
passing an int to it. However, unless your droplet class also declares
a default constructor the 2nd line won't work because the vector tries
to create 200 droplets via the default constructor. Since you provide
at least one constructor, the compiler won't generate a default
constructor for you. But you can initialize the vector like this:

vector<droplet> drops(200,droplet(1,2,3));

Now, the vector contains 200 droplet objects which are copies of the
temporary droplet object that is passed as 2nd argument.

Alternativly, you can write

vector<droplet> drops;
for (int i=0; i<10; ++i)
drops.push_back(droplet(1,2,3));

Get a decent C++ book and read it, seriously. Search the web for C++
book reviews and pick one that suits you best. This way you can save a
lot of time in the long run. Or, you could just continue this trial
and error. But you're likely to shoot yourself in the foot if you do.
Your call...

Cheers,
SG
 
S

SG

Wheter I use -> or . i get an error

Yes. That's because the expression on the left hand side was actually
a vector.
So to get back to my first trial how should my initial
declaration look like?

droplet drops[200];

results in error.

What error? Where? Why didn't you say which error? You're making it
difficult to help you.

I'm *GUESSING* from the *FRAGMENTS* of code, that droplet has no
default constructor. This would explain an error.
also

droplet drops(int, int, int);

What's that supposed to mean? Here you're declaring a function named
"drops" that takes three ints and returns a droplet.

Get a decent C++ book and learn the basics!

Cheers,
SG
 
M

Mike

That's not covered any more in my book...to advanced already?
That's the declaration:
droplet **drops = new droplet * [num];//declare a pointer of a
droplet pointer and sets it to an array of droplet pointers

now to avoid looping drops(new/delete as in my first posting) in the
main loop how should i do such?
Thanks & Cheers Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top