Archived

This topic is now archived and is closed to further replies.

initializing arrays in initializationlist at construct time??

This topic is 5589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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]

Share on other sites
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]

Share on other sites
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]

Share on other sites
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]