Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > declare a class pointer

Reply
Thread Tools

declare a class pointer

 
 
Mike
Guest
Posts: n/a
 
      05-01-2010
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(...
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      05-01-2010
On 1 Mai, 08:58, Mike wrote:
> 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[i] = new droplet(1,2,3);
...
for (int i=0; i<num; ++i)
delete drops[i];


2. Dynamically-allocated array of droplet pointers:

droplet** drops = new droplet*[num];
for (int i=0; i<num; ++i)
drops[i] = new droplet(1,2,3);
...
for (int i=0; i<num; ++i)
delete drops[i];
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
 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      05-01-2010
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
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      05-01-2010
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

 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      05-01-2010
On May 1, 2:22*pm, Mike <michael.sg...@gmail.com> wrote:
> 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".
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      05-01-2010
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
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      05-01-2010
On May 1, 3:03*pm, Mike <michael.sg...@gmail.com> wrote:
> 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.
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      05-01-2010
On 1 Mai, 13:22, Mike wrote:
> 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
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      05-01-2010
On 1 Mai, 14:03, Mike wrote:
> 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
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      05-01-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto declare a function pointer to a method in a class? Francois Grieu C++ 5 10-25-2011 04:29 PM
how to declare structure pointer in within a class sharat C++ 5 12-17-2006 12:46 PM
what are the other ways to prevent a class from being subclassed. one way is to declare the class final. srinivas.veeranki@gmail.com Java 20 02-17-2006 12:21 AM
can I declare a class variable inside that class method? John Black C++ 2 06-15-2004 03:24 PM
how to declare array of class pointer Bob Smith C++ 2 01-01-2004 04:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57