Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can two struct/classes refer to each other?

classes refer struct
0
Posted

How can two struct/classes refer to each other?

0

This problem takes many different flavors, but the general issues goes like this: struct xyz { struct abc Abc; // AA }; struct abc { struct xyz Xyz; // BB }; Unfortunately, for this to work, struct abc needs to be moved before xyz, or else, how could line AA work? But wait! That would mean xyz needs to be moved before abc making this circular. One way around this is: struct abc; // CC struct xyz { struct abc* Abc; // DD }; struct abc { struct xyz* Xyz; // EE }; Here, we’ve changed Abc and Xyz into pointers. As well, we’ve forward declared abc in line CC. Therefore, even though abc has still not been defined, only declared, that enough to satisfy the pointers, because there is not yet any code which is going to be dereferencing the pointers, and by the time there is, both struct will have been defined. Of course this new design implies having to dynamically allocate the memory these pointers will point to… unless of course, the self references are through C++ references, which are pos

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123