How should we declare custom comparator function for priority_queue and why?

From what I found from the internet we should make a separate class of compare function and overload the operator function. Please clarify why do we need to declare a class? why operator function should be overloaded? An example of a user defined comparator would be really useful.

This answer on stack overflow should answer your query read comments also you will find more details.