Reputation: 818
I'm using a funciton object to specify a comparison function for map/set:
struct CompareObject {
bool operator()(Node* const n1, Node* const n2) const;
};
As far as I understand defining a set like this will not create any instance of CompareObject and will pretend it is static:
std::multiset<Node*, CompareObject> set;
But in my problem I need to pass an instance of Tree to it for I'm using it in the actual comparision function:
bool
CompareObject::operator()(Node* const n1, Node* const n2) const {
if (tree->getNoOfGood(n1) > tree->getNoOfGood(n2)) return false;
if (tree->getNoOfGood(n2) > tree->getNoOfGood(n1)) return true;
return false;
}
So, I'm adding some fields to CompareObject definition:
struct CompareObject {
Tree& tree; // added
CompareObject(Tree& t); // added
bool operator()(Node* const n1, Node* const n2) const;
};
The issue I'm having is that I don't know how to instatiate this object with definition of the set.
The first thing that comes to my mind is:
std::multiset<Node*, CompareObjects(*this)> shapesMap; // not valid code
but not suprisingly it gives me an error: ‘this’ cannot appear in a constant-expression
Do you have any ideas how to go around this problem?
Upvotes: 3
Views: 893
Reputation: 944
You can pass in an instance of the functor as a parameter to the set constructor. So something like multiset<Node*, CompareObject> shapesSet(CompareObject(myTree));
Upvotes: 4