new Array of pointer objects

This is a discussion on new Array of pointer objects within the C++ Programming forums, part of the General Programming Boards category; I want to make an pointer array of objects. Is this right? Code: *Enemy = new Enemy_1[4]; Is this right? ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    180

    new Array of pointer objects

    I want to make an pointer array of objects. Is this right?

    Code:
    *Enemy = new Enemy_1[4];
    Is this right?

    Cheers

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,820
    Enemy is a pointer to an array (of 4) objects of type Enemy_1

    > Is this right?
    It's what you've got.
    Whether it's what you want is another matter.

    If you want an array of pointers to objects, then you're way off.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    >>Enemy is a pointer to an array (of 4) objects of type Enemy_1

    yup yup. THats what i want. just double checking that the syntax is right!

    thx

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'm concerned with the lhs of the statement as originally posted. In particular--How is Enemy declared?

    For example, if Enemy is defined like this:

    Enemy_1 * Enemy;

    followed by the line that is posted, then I think there is a problem. On the other hand, if the statement posted is a typo for this:

    Enemy_1 * Enemy = new Enemy_1[4];

    then everything looks in order. And if Enemy is declared some other way, then who knows without seeing it.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Well.....It's declared as such:

    Code:
    Enemy_1 *Enemy = new Enemy_1[4];
    Then I use it...
    Code:
     i=0;
    
            for(i=0;i<5;i++)
            {
                    Enemy[i]->Enemy1_Sprite->Picture->LoadFromFile("ship-5.bmp");
            }
    and I get this erorr...
    Code:
    [C++ Error] Lvl1.h(20): E2288 Pointer to structure required on left side of -> or ->*
    So i changed the declaration to...
    Code:
    *Enemy = new Enemy_1[4];
    Which version is right?

    Thx for the help!

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Try:
    Code:
    Enemy_1 *Enemy = new Enemy_1[4];
    for(i=0;i<5;i++)
    {
        Enemy[i].Enemy1_Sprite->Picture->LoadFromFile("ship-5.bmp");
    }
    Enemy_1 is an array of objects. You access an object's data using '.', so when you want to access the data of the object at index i (using [i]), you have to change the '->' to '.'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 03:45 PM
  3. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 12:07 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. array of pointer objects
    By Death_Wraith in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 08:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21