declare a class pointer

Discussion in 'C++' started by Mike, May 1, 2010.

  1. Mike

    Mike Guest

    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(...
     
    Mike, May 1, 2010
    #1
    1. Advertising

  2. Mike

    SG Guest

    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 = 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
     
    SG, May 1, 2010
    #2
    1. Advertising

  3. Mike

    Mike Guest

    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
     
    Mike, May 1, 2010
    #3
  4. Mike

    Mike Guest

    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
     
    Mike, May 1, 2010
    #4
  5. Mike

    Öö Tiib Guest

    On May 1, 2:22 pm, 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();
    > ...
    >
    > }
    >
    > 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".
     
    Öö Tiib, May 1, 2010
    #5
  6. Mike

    Mike Guest

    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
     
    Mike, May 1, 2010
    #6
  7. Mike

    Öö Tiib Guest

    On May 1, 3:03 pm, Mike <> 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.
     
    Öö Tiib, May 1, 2010
    #7
  8. Mike

    SG Guest

    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
     
    SG, May 1, 2010
    #8
  9. Mike

    SG Guest

    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
     
    SG, May 1, 2010
    #9
  10. Mike

    Mike Guest

    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
     
    Mike, May 1, 2010
    #10
  11. Mike

    SG Guest

    On 1 Mai, 16:59, Mike wrote:
    > That's not covered any more in my book...to advanced already?


    No, it's not advanced. This is rather basic stuff.

    Cheers,
    SG
     
    SG, May 1, 2010
    #11
  12. Mike

    Mike Guest

    for a dll. when i omit new/delete after the first loop i get a memory
    fault
     
    Mike, May 1, 2010
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    20
    Views:
    2,203
  2. Bob Smith
    Replies:
    2
    Views:
    2,724
    Jeff Schwab
    Jan 1, 2004
  3. John Black
    Replies:
    2
    Views:
    372
    jeffc
    Jun 15, 2004
  4. sharat
    Replies:
    5
    Views:
    389
    John Carson
    Dec 17, 2006
  5. Francois Grieu
    Replies:
    5
    Views:
    301
    Krice
    Oct 25, 2011
Loading...

Share This Page