initializing arrays in initializationlist at construct time??

Started by
2 comments, last by Seriema 21 years, 6 months ago
Hello!! Well, tried to cram in as much info as I could to the topic. Lets see if someone got it I have this:
  
class cWarehouse
{
  cWarehouse();
// stuff

  cStorage m_Storage;
  cAutoTruck m_AutoTruckSolo;
  cAutoTruck m_AutoTrucks[5];
};
  
Both my cStorage and cAutoTruck has a constructor that takes one parameter, a cWarehoue pointer. This because I want them to know who "owns" them, and so they can access their "brothers". Well, this works:
  
cWarehouse::cWarehoues()
  : m_Storage(this), m_AutoTruckSolo(this)
{
// stuff

}
  
But how do I do the same with the array of cAutoTrucks??? I''ve tried everything... (well, aparently not everything since it''s not working. Assuming there IS a way to make it work..) And yes I know I could do a cAutoTruck::SetWarehouse(cWarehouse* p_pWarehouse), but I don''t want to! Please help! Thanx... "No lies of sugar can sweeten the sournes of reality" }+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
Advertisement
anyone?..

I can''t find anyone who knows how to do this.

Also noticed that my question is very similar to this:

  int* i = new int[50]( parameter );  


Isn''t that possible either?..

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
I don't believe there is a way to get elements of an array to construct using a constructor other than the default constructor. I think you'll need to create a container of your own. Perhaps like the one below.


  class cAutoTruckContainer : public std::vector<cAutoTruck>{public:    cAutoTruckContainer(int size, cWarehouse *owner)  {    for(int i=0; i<size; i++)      AddAutoTruck(owner);  }private:  void AddAutoTruck(cWarehouse *owner)  {    cAutoTruck autotruck(owner);    push_back(autotruck);  }};    



And use it like so...


    class cWarehouse{  cWarehouse();// stuff  cStorage m_Storage;  cAutoTruck m_AutoTruckSolo;  cAutoTruckContainer m_AutoTrucks;};...cWarehouse::cWarehoues() : m_Storage(this), m_AutoTruckSolo(this), m_AutoTrucks(5,this){// stuff}    


And keep in mind that if copying a cAutoTruck is non-trival you'll need to define a copy constructor.

[edited by - Solo on October 4, 2002 12:10:52 AM]
// Ryan
hey thanx for the tip

I still having trouble that this simple and yet useful task isn''t possible?? Gonna write Stroustrup an e-mail

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]

This topic is closed to new replies.

Advertisement