initialising dynamic arrays with an argument

Started by
11 comments, last by Jaggy 20 years ago

Right so...I don''t need to delete vectors when I''m finished with them? I think I will be okay now. Thanks for helping.
Advertisement
Another way around that would be to make a Create(...) function in your cBlock class and pass any parameters to it.

for example:

m_Blocks = new cBlock[NUMBEROFBLOCKS];for(int i = 0; i < NUMBEROFBLOCKS; i++)     m_Blocks.Create(m_Display);<br><br>  </pre>   <br>   <br><br><SPAN CLASS=editedby>[edited by - OneBitWonder on April 15, 2004 9:01:41 PM]</SPAN>
//in your headder:class cBlock{   //your code...   //my code:   static std::vector<cBlocks*> Blocks;   int InstanceID;};//in your codefile:std::vector<cBlocks*> cBlocks::Blocks;//in your constructor:for(int i=0;i<Blocks.size();i++)   if(Blocks[i]==NULL)   {      Blocks[i]=&this;      goto GoOn;   };Blocks.push_back(&this);GoOn:InstanceID = i;//... continue with your code//in your destructor:for(int i=0;i<Blocks.size();i++)   if(Blocks[i]==&this)      Blocks[i]=NULL;//... continue with your code



You can now access a cBlock-Instance by using
(*cBlock::Blocks[numberOfThatInstance])


Several pro's are given to you by using this technique, ie. you can create cBlocks from anywhere, destruct em from anywhere and access em from anywhere. Also it is fully authomatic done and dynamically generated.


edit: damn html^^


[edited by - fooman on April 16, 2004 3:10:05 AM]

This topic is closed to new replies.

Advertisement