Good day, dear experts. I just got on the warpath with ignorance of C ++, so please treat with condescension if the question is stupid.
The essence of the problem is the following: the class "Product" may consist of 1 or several classes "SedUnitsa", and / or 1 or several classes "Detail". The “Unit” class, in turn, consists of 1 or several “Detail” classes.
The first thing that came to mind is to create, for example, in the class “SbUnitse” a property-array from the classes “Detail”. But this idea I have failed. Something I didn’t think, with the designers, apparently, was sitting for a long time, and now completely confused, I appeal to your help: How to organize an array of classes correctly. If it is possible a tiny example would be ..)

Thanks in advance for good advice)

  • @cppNoob Don't forget to accept (by clicking on the check mark) the best answer. - Nicolas Chabanovsky

3 answers 3

As I understand it, you will need a fairly simple class structure (this whole technique is called aggregation). There will be problems with understanding, then write - I will clarify, I will answer.

class Деталь { }; class СбЕдиница { Деталь* pДеталь; public: СбЕдиница(int n) { pДеталь = new Деталь[n]; } }; class Изделие { Деталь* pДеталь; СбЕдиница* pСбЕдиница; public: Изделие(int nДет, int nСбЕд) { pДеталь = new Деталь[nДет]; pСбЕдиница= new СбЕдиница[nСбЕд]; } }; 
  • That's what I meant :) Nesting like a nesting dolls :) - metazet
  • thank you so much. now I understand. the snag was in pDetail = new Detail [n]; I realized this action by the method of "scratching my left ear with my right hand, passing my hand between my legs" again, thank you so much again) - cppNoob
  • please :) - IAZ
  • structure of <b> objects </ b> classes - AlexDenisov

So what is the essence of the complexity? Well, yes, make the product a private list of assembly units, and make a list of parts for the assembly unit. You can still just use an array. However, it is not clear what the problem is ..

    I think the essence of the problem is how to make this array. Simply declare the link of the desired type and then manually allocate memory for the array itself. Those. eg:

     СбЕдиница *array; 

    (where Unit is the name of your class).