Now, I am trying to build a project, I need to expand an array of pointer to classes. Moreover, this array includes some elements I want to delete them. So, I create another array, copy the elements which i want to keep and copy this array to the original one as follows
Expand|Select|Wrap|Line Numbers
- This function to create two arrays it depends on the parameter x.
- void Econ::CreateFirms(int array,int x)
- {
- if(x==1)
- {
- FirmArray= new Firm*[array];
- if(FirmArray==NULL)
- {
- cout<<" Memory allocation fail.\n";
- exit(1);
- }
- for(int i=0;i<array;i++)
- {
- FirmArray[i]=new Firm();
- //cout << endl<< FirmArray[i];
- if(FirmArray[i]==NULL)
- {
- cout<<" Memory allocation fail.\n";
- exit(1);
- }
- }
- }
- else if(x==2)
- {
- FirmArray1= new Firm*[array];
- if(FirmArray1==NULL)
- {
- cout<<" Memory allocation fail.\n";
- exit(1);
- }
- for(int i=0;i<array;i++)
- {
- FirmArray1[i]=new Firm();
- //cout << endl<< FirmArray[i];
- if(FirmArray1[i]==NULL)
- {
- cout<<" Memory allocation fail.\n";
- exit(1);
- }
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- for(int i=0;i<F;i++)
- {
- FirmArray[i]=FirmArray1[i];
- }
when deleting the FirmArray1