How would you resize an array without using vectors? The array I want to resize is a pointer to a class
class A
{
private:
B * b;
int numOfItems;
int maxNumOfItems;
public:
A();
~A();
resizeMX();
};
A::A()
{
numOfItems = 0;
maxNumOfItems = 20;
b = new B[maxNumOfItems];
for(int i=0;i<maxNumOfItems ;i++)
{
b[i] = 0;
}
}
A::~A()
{
for(int i=0;i<numOfItems;i++)
{
delete [] b;
}
}
void A::resizeMX(const B & obj)
{
bool value=false;
if(numOfItems<=maxNumOfItems && value == false)
{
//assign values to *b in for loop
}
else
{
//resize index of *b
I know that we are supposed to dynamically allocate new memory. Something like this?
++maxNumOfItems;
b=new B[maxNumOfItems];
//keep previous assigned values and add new values at the end
for(int j=numOfItems;j<maxNumOfItems;j++)
{
//assign values to b[j]
}
}
numOfItems++;
}
assume that I did overload the = operator
~A()should only call the delete once.delete [] b;