Function Overloading

Started by
4 comments, last by InsaneX 20 years, 2 months ago
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)
Advertisement
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
easier than using templates?

___________________
Sketchworx Studios
___________________Sketchworx Studios
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.
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]
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

This topic is closed to new replies.

Advertisement