Not sure

Is there any reason I shouldn't use the following code I've written?
The IDOs are defined in a seperate header.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void ObjectController::releaseObject(void* object, int objectType)
{
	switch(objectType)
	{
		case IDO_Background:
			((ObjectBackground*) object)->release();
			break;
		case IDO_Box:
			((ObjectBox*) object)->release();
			break;
		case IDO_Button:
			((ObjectButton*) object)->release();
			break;
		case IDO_GfxStats:
			((ObjectGfxStats*) object)->release();
			break;
		case IDO_Ship:
			((ObjectShip*) object)->release();
			break;
		//default:
	};
}


Thanks.
You have a set of classes names Object<something> - are they related?
If they are not curently, could they all descend from a single parent class?
If so, rather than use void* you could use <parent class>* as the parameter, then just call object->release() and let polymorphism take care of calling the right one for you.
Much cleaner code, and easier when you later add a new Object type as it will be handled in this routine automatically.
See http://www.cplusplus.com/doc/tutorial/polymorphism.html for info on polymorphism if you need additional info - it's a very powerful feature of OO.
No, they aren't much related. I considered making a parent class, but there isn't even one element or method every object has.. other than release() come to think of it >_>

Hmm, I may do some reconstruction then.

However I am still curious, is there anything fundamentally wrong with using the above code?
IMO you can write your programs anyway you wish, however to me it
does raise some questions

Q1
if ObjectController is the master for allocating and deallocating IDO objects (it's name give the impression that it is)
why when releasing objects do you have to give it both the pointer to the object
and tell it what type of object of object is being released?
Maybe if ObjectController returned a HANDLE when it created an object, then, when you want to relase the object - pass the HANDLE to the ObjectController, and ObjectController releases the object, - the IDO wouldn't need it's own release function.

Q2
Out of interest - What does the release function do.
Is the IDO object trying to use the delete operator on itself.
(This is legal by the way).

My summary is - it depends on how IDO object are allocated and by whom.

Maybe some of the more experienced programmers can expand on this.


To sixequalszero:

It wouldn't be first time nor the last that an (abstract) parent class contains only a single pure virtual method. I would agree with Faldrax to use inheritance/derivation as a perfectly acceptable solution to the problem, even if release() is the only common part.

1
2
3
4
class Releasable {
    public:
        virtual void release() = 0;
};

Thanks,
I have implemented,
1
2
3
4
5
class Object
{
public:
	virtual void release()=0;
};

with all my objects derived from it.

However I am having problems with allocations, which I had previously here,
http://www.cplusplus.com/forum/general/4228/
What problems are you having? (I replied to the referenced thread already).
Topic archived. No new replies allowed.