简体   繁体   中英

C++ dynamically increase array size for object-array?

I'm making a game with cocos2d-x. The different objects in the game i want to store in a class (I dont know if this is a good idea, but so i can give every object a lot of attributes). Then i make an array out of the objects and for that i need an own datastructure, where i can push and pop my objects. I tried to write this datastructure, but i think i doing wrong with my push function (i want to dynamically increase array size), especially the delete []? Doesn't that destroy my object-pointers stored?

ObjectArray.h:

#pragma once

#include "C:\Cocos\Projects\FirstGame\proj.win32\anObject.h"

class ObjectArrayList
{
public:
    ObjectArrayList(int c);
    ObjectArrayList();
    virtual ~ObjectArrayList(void);

    void push(anObject *obj);
    void pop(int id);
    int findIndex(int id);
    int getSize();
    int getCapacity();

private:
    int capacity;
    int size;
    anObject **objectList;
};

ObjectArray.cpp:

#include "ObjectArrayList.h"
#include <iostream>
using namespace std;

objectArrayList::ObjectArrayList(int c)
{
    size=0;
    capacity = c;
    objectList = new anObject*[capacity];
}

ObjectArrayList::ObjectArrayList() {
}


ObjectArrayList::~ObjectArrayList(void) {
}

void ObjectArrayList::push(anObject *obj) {

    if(size < capacity) { 

    } else {
        int newCap = 2*capacity;
        anObject **tmpObjectList = new anObject*[newCap];
        for(int i = 0;i<capacity;i++) {
            tmpObjectList[i] = objectList[i];
        }
        delete [] objectList;
        objectList = tmpObjectList;
        capacity = newCap;
    }

    objectList[size] = obj;
    size++;
}

void ObjectArrayList::pop(int id) { //not finish yet
    if(size != 0) {
        size--;
    }
}

int ObjectArrayList::findIndex(int id) {
    return id; 
}

int ObjectArrayList::getSize() {
    return size;
}

int ObjectArrayList::getCapacity() {
    return capacity;
}

anObject.h:

#pragma once

#include "cocos2d.h"

class anObject
{
public:
    anObject(int hp_init, int x, int y);
    anObject();
    virtual ~anObject(void);

    void decreaseHp();
    int getHp();
    void setMyPosition(int x, int y);
    cocos2d::Sprite *getMySprite();


private:
    cocos2d::Sprite *mySprite;
    int hp;
    int midX;
    int midY;
    int isX;
    int isY;
};

Bert

As the comments pointed out... you should save some time and energy and try to use the Standard Template Library (STL) .

If you insist on fixing this code, I think you should try referencing objectlist after the delete to see if it's still there... maybe this assignment...

objectList = tmpObjectList;

...is not tolerated.

Instead... try to build a copy constructor "public Object(Object copiedObject){}" , make a new Object*[] of the 2X size, populate it, then get rid of the old and... without deleting objectList assign your new Object*[] to it...

The rest seems fine to me... hope this helps.

delete [] objectList;

This only frees the array which objectList points to. It does not free any of the objects in that array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM