Results 1 to 4 of 4

Thread: Dynamic Arrays

  1. #1

    Thread Starter
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620

    Dynamic Arrays

    k i'm basically replicating the vector class.
    I have ALOT of code which uses the fixed array of 10 elements. I have two classes being used also. The dynamic array would be used in a class.
    So so far this is what i have for the fixed Private section of the class:
    static const int MAX = 10;
    T data[MAX];

    and then within the class implementation, i have numersoun functions that use this array. My question is: wat would be the easiest way to convert this class to a dynamic array.

    Would i change the above code to:
    static int MAX = 10;
    T *data;

    and then would i have to sort through the class and wherever there is an element being added to the erray i would check for the current size of the array and if it's too small i would use the 'new' to increase the size of the array?
    T *data = new T(MAX + 10);
    would taht work?

    Someone please help, there is just SO much code that i can't figure this out.

    Thanx,
    D!m

    PS. Sorry for the length.
    Dim

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    static int MAX = 10;
    That would cause all instanced objects to have that size, i think you mean to leave out static.
    would taht work?
    you would have to keep count on the amount of allocated memory as well, or alternatively check if the modulus with 10 is 0

    remember to delete[] your array in the destructor or whenever you resize it
    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

    Thread Starter
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    I would have to initialize the MAX in one of the constructors right, can't do it in the class def?
    Dim

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    yep
    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