For example, I have two classes
class Foo;
class Bar;
class Foo {
const Bar &m_bar;
...
};
class Bar {
const Foo &m_foo;
...
};
Let foo is object of Foo and bar is object of Bar. Is there any way (normal or "hacking") to create/initialize foo and bar that their members m_bar and m_foo would referenced to each other (I mean foo.m_bar is bar and bar.m_foo is 'foo')?
It is allowed to add any members to Foo and Bar, to add parents for them, to make they templates and so on.
What is the linkage of
fooandbar? If they have external linkage, you can write something like:(I'm assuming here that it is the constructors which set the reference to a parameter.)
This supposes namespace scope and static lifetime, of course (but an anonymous namespace is fine).
If they are class members, there's no problem either:
If they're local variables (no binding), I don't think there's a solution.
EDIT:
Actually, the local variables have a simple solution: just define a local class which has them as members, and use it.