In a C++ program, I have a struct that contains a pointer to another instance of its own type:
struct Foo
{
const Foo* ptr;
};
I want to declare and initialize two const instances of this struct that point to each other:
const Foo f1 = {&f2};
const Foo f2 = {&f1};
However, this results in the compile-time error error: 'f2' was not declared in this scope
(obviously because f2 is declared after f1, even though f1's declaration references it).
Is what I'm trying to do possible and reasonable? If it is, how do I make it work?
One workaround might be to avoid making f1 const, then reassign the pointer after f2 has been declared (f1.ptr = &f2;
), but I'd rather avoid this if I can.