Help with dynamic array in class definition

Discussion in 'OT Technology' started by wumpusHunter, Feb 1, 2008.

  1. wumpusHunter

    wumpusHunter New Member

    Joined:
    Nov 24, 2007
    Messages:
    24
    Likes Received:
    0
    I'm trying to make a dynamic array a private member variable of a class, where the size of the array is also a private member variable of the same class. I can't seem to get the syntax correct in the class definition. Is this even allowed?

    Any help appreciated!
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    is this C++? youd declare the member variable as a pointer and have the size as a #define or const or whatever you want. then in the constructor you can do

    name = new char[MYCONST];

    if name was declared as a char *.
     
  3. wumpusHunter

    wumpusHunter New Member

    Joined:
    Nov 24, 2007
    Messages:
    24
    Likes Received:
    0
    Sorry, I should've specified... Yes, this is in c++.

    I think that the problem is that the size of the array isn't a constant. It's a member variable which I was planning on setting before creating the dynamic array.
     
  4. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    would doing something like passing it in as a parameter to the constructor be what you were thinking? youd do it the same way as above.

    Myclass::Myclass(unsigned int arraySize)
    {
    myArrayPtr = new char[arraySize];
    }
     
  5. wumpusHunter

    wumpusHunter New Member

    Joined:
    Nov 24, 2007
    Messages:
    24
    Likes Received:
    0
    I actually just did that. I got it working! It's been a while since I've done any programming... Thanks for the help.
     

Share This Page