Skip to main content
gamedev.net
🔒 Locked

Function Overloading

Started by InsaneX 4 replies 300+ views
Original Post
InsaneX
InsaneX
I was wondering if there was an easier way to overload a function to accept different types of data (int, float), when the type wouldn''t matter. An example of this would be a memory manager, you have a DeAllocate() function, no matter what type of pointer is passed, you want to just delete it, and it won''t matter what type. (this is assuming no arrays)
Desert Fox
Desert Fox
Um, templates?


template <class T>
void DeAllocate(T* ptr)
{
delete ptr;
}
"Is life so dear, or peace so sweet, as to be purchased at the price of chains and slavery?" - Patrick Henry
InsaneX
InsaneX
Thank you very much Desert Fox!! I knew I had read about something like this somewhere, and I was looking through my unorganized tutorials and I couldn''t find it.
_Twiggie_
_Twiggie_
And possibly another good solution would be using a void *, which accepts any datatype (and furthermore you could cast them to an int, float, etc)


void DeAllocate(void *ptr)
{
delete ptr;
}


[edited by - _twiggie_ on January 24, 2004 7:48:26 AM]
Desert Fox
Desert Fox
Except in that case a destructor never gets called if ptr points to an object and not a primitive =)
"Is life so dear, or peace so sweet, as to be purchased at the price of chains and slavery?" - Patrick Henry

Topic Locked

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

Sign in to reply to this topic.