Basically, I have a templated class that runs some algorithm, and that algorithm needs a similarity(T t1, T t2) function that returns a double defining how similar two objects of type T are. This similarity function varies greatly depending on what T is defined as, so the caller of this class needs to define the similarity function. I assume a function pointer is in order, but how do I save a function from a function pointer as a member function for use in the algorithm?
That is, I want to pass in similarity(T t1, T t2) in the class’ constructor and be able to call that as a member function throughout the class. How do I do that? Thanks
You can’t add member functions to a class at runtime.
You could write a member function that calls the supplied function via the function pointer:
Alternatively, you could make the
similarity_fnmember public. The syntax for calling it would make it look like a member function, but you might not consider it to be great encapsulation.