Hello,
every reply here is "out of my head" :-(
That's probably not a problem. If you really need to think on that
problem when programming, you will get it right, in a natural way, if
you know enough C++.
We had a selection of problems in this thread where that obscure thing
of initializing something using its own name might happen. We don't
know what the author had in mind, and why he expected somebody working
through the book being able to give a reasonable answer, whether it was
meant as tricky question or whatever.
I first thought the OsThingaMajick answer by Alf P. Steinbach is closest
to the intended, because it matches the content of the chapter best,
but it does not use the name of an object in its initializer, but its
type. So I think it is not precisely an answer to the question.
But expanding on his, you might want a pointer to itself within a
struct. E.g. a linked list, where the next pointer points to itself in
the last node.
struct intlistnode
{
int v;
intlistnode * next;
};
extern intlistnode n1;
extern intlistnode n2;
extern intlistnode n3;
intlistnode n1 = {2,&n2};
intlistnode n2 = {3,&n3};
intlistnode n3 = {4,&n3};
int main()
{
if (n1.next->next != n1.next->next->next) throw n1;
}
Now you have a list of integers arranged by the compiler, starting with
n1. And you can check you have reached the end node by testing whether
next points to the node itself. Others might prefer using a zero
pointer to signal this.
Bernd Strieder