Home Game Development c++ – Unable to copy object contents?

c++ – Unable to copy object contents?

0
c++ – Unable to copy object contents?

[ad_1]

so my code is the following:

template<typename ITEM>
struct XMAP {
    XMAP() {
        this->size = 0;
        this->length = 0;
        this->block = 8;
        this->package = nullptr;
    }

    struct PACKAGE {
        PACKAGE() {
            this->exist = false;
        }
        ITEM item;
        bool exist;

        void operator = (ITEM i) {
            this->item = i;
            this->exist = true;
        }
    };

    int size, length, block;
    PACKAGE* package;
    PACKAGE dummy;

    void Resize() {
        this->size += this->block;
        if (this->length > 0) {
            PACKAGE* package = new PACKAGE[this->size];
            for (int i = 0, c = 0; i < this->size; i++) {
                if (this->package[i].exist) {
                    package[i] = this->package[i].item;
                }
            }
            delete[] this->package;
            this->package = package;
        }
        else {
            this->package = new PACKAGE[this->size];
        }
    }

    void Copy(XMAP<ITEM> &x) {
        x.package = new PACKAGE[this->size];
        x.size = this->size;
        for (int i = 0; i < this->size; i++) {
            if (this->package[i].exist) {
                x.package[i] = this->package[i].item;
            }
        }
    }

    void Position(ITEM item, int pos) {
        for (; this->size <= pos;) this->Resize();
        this->package[pos] = item;
    }

    ITEM operator [](int pos) {
        if (pos < this->size) return this->package[pos].item;
        return this->dummy.item;
    }
};

And the problem is, I can’t seem to get the contents to copy when using the Resize function. I’ve tried just about everything from making the XMAP::PACKAGE::item a pointer, to copy-constructors. And i just can’t seem to pinpoint what i’m missing.

[ad_2]