What is the linkage of foo
and bar
? If they have external
linkage, you can write something like:
extern Foo foo;
extern Bar bar;
Foo foo( bar );
Bar bar( foo );
(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:
class Together
{
Foo foo;
Bar bar;
public:
Together() : foo( bar ), bar( foo ) {}
};
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.