HPM HPM - 4 years ago 100
C++ Question

Can you make a C++ generic function?

Is it possible to create a generic C++ function

foo
?

foo(Object bar, Object fred)
{
//code
}


in which that if the two objects are recognized, they are compared and a comparison value is returned otherwise some other value is returned to indicate a comparison was not possible?

I ask in the case of genericizing a sorting class, in which case you can use this method, and when you derive new objects you want to sort, you add to this foo function, a method on which to sort the new type of Object.

Answer Source

I think you are in dire need of Templates!
You can write a template function and then write a specialization for the said types to do something specific if the need be.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download