dynamic array and losing data of the first element
Hello! :)
Here's my problematic code:
Code:
class konto
{
private:
char *nazwa;
int size;
papier *tabl;
public:
~konto();
konto(char *);
void dodajPapier(papier &);
void zapiszWplik(const char *nazwaPliku);
};
konto::konto(char *nazwa)
{
this->nazwa = new char [len(nazwa)];
strcpy(this->nazwa, nazwa);
this->size = 0;
tabl = new papier[size+1];
}
konto::~konto()
{
delete [] tabl;
}
void konto::dodajPapier(papier &akcja)
{ //first element in the array still exists
tabl[size] = akcja;
//here tabl[0] points somewhere else but not to 0-element
papier *temp = new papier[++size];
for(int i = 0; i < size; i++)
temp[i] = tabl[i];
tabl = temp;
}
When I add second object of papier the value of tabl[0] is lost.. Next objects are created properly in the array.
Why's that ? I can't fix it even though I know where it happens :-/