Hi, I am learning Class and I want to write a code that insert value into an array that have been allocated dynamically. I want my program to increase memory allocation of the array by *2 if the current size of the array is full, and also copy the precedent value of the array into the bigger one. (I want to do this manually and not use vector).
Here is the code I wrote, respecting rule of 3. All is working but when I create the function alias my program doesn't work anymore. I spend lot of time to try different possibility, but don't find something that work. Can you explain why, and how can I correct code.
As @jonnin hinted, line 76 assigns this->database. However, any previous value of this->database gets leaked.
Another thing... why do you have an initialization list for _database and then immediately overwrite it in the first line of your constructor (first 2 constructors). Either put all 3 lines into the initialization list, or put all 3 into the body. The initialization list is probably better.
In the 3rd constructor--your copy constructor--you know that this->database is not valid because you are constructing this object. There is no need to check for null. And it's a constructor, so there is no need to delete the old this->database (line 21). That's only needed for an assignment operator (which is what alias essentially is).
And why are you creating tmp in either place? Just delete (if necessary) and set this->database = new int[size];
One more thing: in your second constructor, what do you do if the argument is 0?
int* tmp = newint[source._size]; //ok, get a temp variable and all that looks fine.
for (int i = 0; i < source._currentSize; i++)
//tmp is sized off source.size but iterates off currensize. is that messed up? could be right, i got
// no idea, but its not what you allocated.
*(tmp + i) = *(source._database + i); //why do this to the reader? how about tmp[i] = source.database[i]; ?!
this->_database = tmp; //old database memory, if any, is thrown away and leaked.
this->_currentSize = source._currentSize; //ok, now they match. but size vs currentsize is still confused above.
this->_size = source._size; //ok