Consider a class A, STL container B and a binary predicate C for the container.
Container B is used in class A. But class A is also used in the binary predicate C.
struct C{
bool operator()(A const &a, A const &b){
return a.compare_variable < b.compare_variable;
}
};
We need this predicate in order to define the container, which uses it to order its elements.
Because the container declaration becomes rather long, I used typedef to simplify the declaration.
typedef B<A, vector<A>, C> type;
Finally, my goal is to declare the container B ---whose declaration is abbreviated to "type" --- inside the class A. Namely, as a static public member variable.
class A{
public:
type container1, container2;
};
What is the right order is which to declare A, B and C?
I tried the following variations in the order:
first declaring the class A, then the struct C and at last the typedef, I get the error that container1, container2 do not name a type --- type did not exist at the time of the class declaration;
first the typedef: loads of errors --- both the class and struct are not defined yet;
- first declaring the class, with the typedef in the public: section the the struct: an error saying that the third template argument (C) is invalid --- it has not been defined yet;
- first declaring the struct: error saying the the class is not defined yet.
Is the method that I use unnecessarily cumbersome and does there exist a more elegant solution?