Results 1 to 5 of 5

Thread: arrays of objects

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    arrays of objects

    I want to declare a global array of objects, but i don't know how many objects there will be until later in the program. how would i do this?
    thanks

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    you declare a pointer to the object.

    to allocate an deallocate memory dynamically you use new and delete.

    ex

    obj* g_myobjarray;
    ..
    g_myobjarray=new obj[x];
    ..
    delete[] g_myobjarray;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Or you declare an STL vector, which is easier to use.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    Question Passing Arrays Of Objects ByRef

    say i do something like this:
    Obj *myObj;
    myObj = new Obj[10];

    Now, myObj is a pointer right? So if I wanted to pass the array byref to a function, would i have to pass the memory address of the pointer myObj to a function that accepted the value as another pointer? Like:

    void blah(Obj *pointer) {}
    blah(&myObj)

    or could I just pass the pointer myObj and not the mem address? Also, is there any way for 'blah' to tell the upper bound of the array that was passed? What is the correct argument syntax for passing an array?
    thanks
    jmiller

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I suggest you do as CB suggested and have a look at the vector if you want a fast and easy solution, if you're practicing C++ arrays, pointers and references can be very useful to understand

    myObj is a pointer to the first object in the array, if you want to reassign or resize the array you'd better pass the pointer by reference, Obj *&pointer, and when you declare a reference, you don't have to use the addressof operator, so

    void blah(Obj *&pointer) {}
    ..
    blah(myObj);

    if you only want to change or access of the array elements, you could just pass the pointer, but you cannot change the pointer (you can but only in this scope.)

    void blah(Obj *pointer) {}
    ..
    blah(myObj);

    The size of the array has to be passed as well in a separate variable, if you intend to resize it. Remember for each new you have to use delete, and be cautious to new and delete in separate scopes, you'll easily end up with memory leaks, its not considered as good programming to do so.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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