What I am trying to do is to place a class object in the buffer and then be able to reference it correctly later.Essentially its a container class using a buffer for data storage.The best way I thought of doing so was storing the object's address in the buffer, reference it by its index, and then cast it. I see now by doing it that way can potentially leave memory leaks because the object is only living locally in this method and the address of that local object is being returned. Is there a way I can store the object into the buffer and have it correctly referenced later by invoking overloaded operator[] Foo[index]? I have tried using the same technique with C++: Casting an Object to char* for saving/loading but static/re-interpret cast in my case are tending to change the address values when I attempt to do an address look up for the contents in the buffer.
ps. I know that using a vector would be an easier way of storing class objects but part of the restriction is that I can't use STL for data storage and have to rely on the buffer given to me.
#include <stdlib.h>
#include <assert.h>
#ifndef FOO_H_
#define FOO_H_
template <typename T>
class Foo {
char * oBuffer = NULL;
unsigned items = 0, bSize = 0;
public:
Foo(char * pBuffer, unsigned nBufferSize) :
oBuffer(pBuffer),
items(),
bSize(nBufferSize){
/*for (unsigned i =0; i < nBufferSize; i++)
oBuffer[i] = &pBuffer[i];*/
}
~Foo(){ delete[] oBuffer;}
T * Add(){ ///====== Adds an element to the container, constructs it and returns it to the caller.
assert(Capacity() > Count());
T nObj; // new object
T *nElement = &nObj; // address of new object
oBuffer += items; // incrementing pointer by item count
oBuffer = (char*) nElement; // attempt to store object address in buffer[items] location
items++; // increment items count by one
return (T*) &oBuffer;
}
T * operator [] (unsigned nIndex){ ///====== Returns the n th element of the container [0..Count -1].
return (T*) (&oBuffer[nIndex]);
}
};
#endif
Originally I was trying to do the add as follows:
T * Add(){ ///====== Adds an element to the container, constructs it and returns it to the caller.
assert(Capacity() > Count());
T *add =&(oBuffer[items++] = T{});
return add;
}
But I would come into problems when T = was a custom class object.