0

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

7
  • 2
    Is std::vector<anObject*> objectList; not an option? Commented Aug 24, 2014 at 7:17
  • 1
    anObject **objectList; Why don't you use a std::vector<<std::vector<anObject> > objectList; instead? Commented Aug 24, 2014 at 7:19
  • 4
    From his description, I think he just needs std::vector<anObject>. Unless the virtual destructor is an indication that he plans to have runtime polymorphism, in which case, std::vector<std::unique_ptr<anObject>>. Commented Aug 24, 2014 at 7:25
  • @πάνταῥεῖ What's wrong with push() here? It looks fine to me... Commented Aug 24, 2014 at 7:29
  • @BenjaminLindley 'From his description, I think he just needs std::vector<anObject> ' Could well be. Commented Aug 24, 2014 at 7:29

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
delete [] objectList;

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

1 Comment

Thanks, i was really not sure about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.