Re: How do I get around circular references in C++?
On 21 Maj, 14:27, "michael" <s...@begone.net> wrote:
Hi All,
This is probably not the best example of this but it illustrates my point.
What I have is:
class Obj {
private:
string some;
string stuff;
public:
set<Obj, CompareObjs> ObjSet;
};
class CompareObjs {
public:
bool operator ()(Obj& lhs, Obj& rhs){
return true;
}
}
so each of these classes has a reference to the other, and they appear in
the same file.
No matter what order they appear in one of them is not happy.
Is it possible to get around this without having two files?
Separate the declaration and definition of CompareObjs::operator() and
make a forward declaration of Obj:
class Obj; // Forward declaration
class CompareObjs {
public:
bool operator ()(Obj& lhs, Obj& rhs);
};
class Obj {
public:
std::set<Obj, CompareObjs> ObjSet;
};
bool CompareObjs::operator ()(Obj &lhs, Obj &rhs)
{
return lhs < rhs;
}
Notice that for this to compiler you need to add an operator< to Obj,
but you can just redefine CompareObjs::operator() to perform the
comparison any way you want.
--
Erik Wikstr=F6m