Skip to main content
GameDev.net gamedev.net
🔒 Locked

Interface Question

Started by beebs1 Feb 19, 2007 at 5:45 PM 4 replies 1.1k views
Original Post
beebs1
beebs1
Hiya, My current project uses an interface architecture, for example:
// IObject.h
class IObject
{
public:
    IObject() {}
    virtual bool Initialize() = 0;
}

// Object.h
class Object : public IObject
{
public:
    Object();
    bool Initialize();
}
I use 'factory' like functions to create objects like this, for example bool CreateObject( IObject **ppObject ); internally creates an Object class instead of IObject. Sometimes I need arrays of objects though, and I'm not sure of the best way to do this. How should I create the array. I was thinking of something like:
bool CreateObjectArray( IObject **ppObject, int iNumObjects )
{
    *ppObject = new Object[iNumObjects];
}
Is that OK, or is there a better way? Thanks for any replies, I hope that was clear enough :)
RidiculousX
RidiculousX
Yes, that is the correct way to allocate an array on the heap. Just don't forget to clean up afterwards with a call to delete[] (notice the opening and closing square brackets).

If an array doesn't suit your needs, you might want to look into linked lists (Wikipedia).
Vorpy
Vorpy
No, this is simply terrible.

The array is being allocated correctly, yes. But everything else is wrong.

First of all, the base class does not have a virtual destructor.

Next, this array is going to cause problems when you try to use it. Let's say we use the function and get an IObject* pIObject. pIObject[1].doSomething(). Oops! The silly compiler thought the array contained IObjects and calculated the address of the second element in the array accordingly! But the array did not contain IObjects, it contains Objects of a different size and the resulting address is incorrect. Try it and see what happens! (Note: if the subclass contains no data members then it might still work slightly, but your virtual functions might not be dispatched correctly).

If you need polymorphic arrays, then the only safe way is to have an array of pointers.
RidiculousX
RidiculousX
Thanks for pointing that out, I hadn't thought of that.

Using an array of pointers to IObject instead of the actual instances of IObjects on the heap would fix that, but then you would have to initialize each heap instance individually (or as an array, but then when you go in to delete things it may get messy) and set each pointer in the array to point to the actual instances.
Vorpy
Vorpy
Another way is to have your factory handle the allocations however it wants, and then fill in the pointer array with pointers to the things the factory allocated. The factory can allocate an array of Objects and keep track of this allocation somewhere, and then allocate an array of pointers to IObjects and set the pointers to point to the elements in the Object array. This requires you to implement low level memory management bookkeeping. These sorts of issues are a cost of using low level C++. I really wouldn't recommend writing something complicated like what I just described unless you really had to...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.