dynamic array of base class pointers

This is a discussion on dynamic array of base class pointers within the C++ Programming forums, part of the General Programming Boards category; For a class project I am trying to make a dynamic array of pointers to the base class and program ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    dynamic array of base class pointers

    For a class project I am trying to make a dynamic array of pointers to the base class and program keeps crashing. I have the base class objectType, and the derived classes balloonType, enemyType, and obstacleType. I define the array of pointers and allocate memory like this.
    Code:
    objectType **m_object;
    
    
    m_object = new objectType*[m_nObjects];
        
            //load the objects
            for (int i = 0; i < m_nObjects; i++)
            {
                string itemType;
                inFile >> itemType;
                
                if (itemType == "balloon")
                    m_object[i] = new balloonType;
                
                if (itemType == "obstacle")
                    m_object[i] = new obstacleType;
    
                if (itemType == "enemy")
                    m_object[i] = new enemyType;
                
                inFile >> *m_object[i];
            }objectType **m_object;
    this however is not where it crashes, it crashes when the base class function GetDepth() is called. The function is a member of objectType, here is the function definition and where it crashes

    Code:
     //inside objectType class
    inline int GetDepth() { return m_depth; }
    
    
    void worldManager::DepthSort()
    {
        //bubble sort by depth - high to low
        for (int i = 0; i < (m_nObjects - 1); i++)
        {
            for (int j = 0; j < (m_nObjects - 1 - i); j++)
            {
                if (m_object[j]->GetDepth() < m_object[j+1]->GetDepth())  
    //******* crashes here, it says that it 
    //cannot access memory for GetDepth()
                {
                    objectType *tempObj = m_object[j];
    
                    m_object[j] = m_object[j + 1];
                    m_object[j + 1] = tempObj;
                }
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > inFile >> itemType;
    Are you sure itemType is filled with either balloon, obstacle, or enemy here? Because if for some reason it isn't, no memory will get allocated by the if() statements. You might print out itemType, just to be sure.

    Code:
    >        }objectType **m_object;
    Is this line just a typo?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. **Pointer to a Dynamic Array of Pointers
    By The Brain in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2005, 07:52 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 09-10-2003, 10:30 PM
  5. Replies: 4
    Last Post: 09-12-2001, 03:05 PM

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