Results 1 to 12 of 12

Thread: Initialising arrays of objects

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Initialising arrays of objects

    Me again.

    When you create an array of objects, how can you pass the arguments required by the constructor?

    example:

    when you declare a single object it might look something like this:
    Code:
    clsComplex Number(2,1);
    but when you declare an array of objects:
    Code:
    clsComplex Number[3];
    How do you also add the arguments for the constructors?
    Using Visual Studio .NET 2005

  2. #2
    Junior Member MikeyD's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    17
    You cannot do it that way.

    You have to allocate You objects at run-time (on heap) through new:
    Code:
    clsComplex* ppComplex[3];
    for (int x=0; x < 3;++x)
    {
    	ppComplex[x] = new clsComplex(2,1); // call ctor with arguments
    }
    Don't forget to delete Your array-ptrs. after use!

    Mikey
    Last edited by MikeyD; Nov 30th, 2002 at 07:36 PM.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I don't know, it should be possible:
    Code:
    object blah[3] = { object(4,5), object(2, 89), object(69, 31337) };
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195
    Both work. Thanks muchly
    Using Visual Studio .NET 2005

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Note that the first is more flexible, while the second is more efficient (and you don't need to delete afterwards).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195
    Just as an addition, since it's kind of related:

    What's up with this:

    Code:
    BYTE bArray[5];
    
    bArray={1,2,3,4,5};
    I can't use this "BYTE bArray[5]={1,2,3,4,5};" because the array values will changes.

    Just to be specific, the code should assign 1 to bArray[0], 2 to bArray[1] etc.
    Using Visual Studio .NET 2005

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can use the second one, since you can still change the array contents.

    It's only impossible to change it if you declare the array as const.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Junior Member MikeyD's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    17
    parksie is right,
    You only need the new-op. if You have to decide at runtime, how large Your array is
    or if You have to change the array-size.

    Mikey

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195
    errr, how?

    (I know I'm a pain)
    Using Visual Studio .NET 2005

  10. #10
    Junior Member MikeyD's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    17
    what You mean - "how"?

  11. #11
    Junior Member MikeyD's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    17
    if You mean with "how", how to assign new values to Your array:
    Code:
    BYTE bArray[5] = {1,2,3,4,5};
    
    bArray[3] = 6; // assign a new value to index 3

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195
    I was replying to parksie's , sorry

    I wanted to know how I could change the contents of the array using the method you both describe.
    Using Visual Studio .NET 2005

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width