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

Functions for multiple parameter types

Started by A l e x Dec 15, 2002 at 10:41 AM 3 replies 850+ views
Original Post
A l e x
A l e x
Hi, I have some classes whose objects need to be tracked with id''s. Now I don''t want to program an id handler for every class. As it''s always the same, it only complicates the code. I want my class to look like this:
  class idhandler
{
protected:
	int lastbuttonid;
public:
	idhandler();
	HRESULT addobject(button newbutton);
	HRESULT deleteobject(button delbutton);
	HRESULT insertobject(button insbutton, int newid);
};
  
How can I use this class for other classes, too? Thank you, Alex
CraZeE
CraZeE
y not try creating the ID_handler class as a base class... every other class inherits from it. that way all ID handling matters are done once, and chances are u probably wont need to modify the base class or improve further in ur inherited classes; assuming of course u''re talking bout basic ID based attach/detach, nothing more.
To learn, we share... Give some to take some -
A l e x
A l e x
I was thinking about that, too, but is there really no way to do something like this? Maybe I''ll want to use this idhandler with no class someday...
CraZeE
CraZeE
u can try the alternative ''singleton'' classes. these are covered wuite well in the game programming gems series, but a search on Google will help too

of course going the singleton way may lead to ugly templatized impementation to ensure ur handler can support all the game objects. a simple way to overcome this is to derive all the objects from a common base class. the handler then technically only tracks all these ''base objects''... useful oop.

but since this involves inheritance anyway, just sticking to the previous post will do just fine. its a tough choice, but either way is fine i guess.
To learn, we share... Give some to take some -
Fruny
Fruny
Something like this ?


    
template<class Type>
class IdHandler
{
protected:
size_t mLastId;
public:
IdHandler();
HRESULT AddObject(Type& obj);
HRESULT DeleteObject(Type& obj);
HRESULT InsertObject(Type& obj, size_t id);
};


IdHandler<Foo> FooHandler; // Takes objects of type Foo


IdHandler<Bar> BarHandler; // Takes objects of type Bar





[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

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

Sign in to reply to this topic.