This question gives answer only when the classes use each other as the member types, but not when they use the functions of each other. How to do it in the following example? Bar
is a singleton container which handles the operations done on all Bar
s.
// Foo.h
#include "Bar.h"
class Foo {
public:
Foo() { Bar::get_instance()->push_back(this); }
void do_something() {}
};
// Bar.h
#include "Foo.h"
#include <vector>
class Bar {
public:
Bar(const Bar &other) = delete;
void operator=(const Bar &) = delete;
static Bar *get_instance()
{
static Bar bar;
return &bar;
}
void push_back(Foo *foo) { foos_.push_back(foo); }
void do_something_all() { for (Foo *f : foos_) f->do_something(); }
private:
Bar() = default;
std::vector<Foo *> foos_;
};