I also have some errors with copy constructor that I cant figure out, but the main question at the moment is brought out in my test programs comments. Why is the second print out so big number, and not three? Thank in advance for your help.
template<class T>
class Array {
private:
T *cont;
unsigned int size;
public:
Array()=default;
T& elementAt(unsigned int i)
{
return cont[i];
}
Array( const Array &a )
{
cont = new int[ size ];
for ( int i = 0; i < size; i++ )
cont[ i ] = a.cont[ i ];
}
Array& operator = (const Array& a)
{
int i,min_size;
if(size < a.size)
min_size = size;
else
min_size = a.size;
for(i=0; i<min_size; i++)
cont[i] = a.cont[i];
return (*this);
}
void addElement(T element) {
T *new_cont = new T[size + 1], *old_cont = cont;
for (int i = 0; i < size; i++) {
new_cont[i] = old_cont[i];
}
new_cont[size] = element;
size++;
cont = new_cont;
delete old_cont;
}
unsigned int getSize() const
{
return size;
}
~Array()
{
delete [] cont;
}
};
And this part of my test program:
Array<int> numbers;
numbers.addElement (5);
numbers.addElement (11);
numbers.addElement (3);
cout << numbers.getSize()<<endl; // Here I get output: 3
cout<< numbers.elementAt(1)<<endl; // output: 11
MyArray<int> copy2;
copy2 = numbers;
cout << copy2.getSize()<<endl; // WHY is output: 2686697, in here? Why isn' t it 3.
cout<< copy2.elementAt(1)<<endl; // output: 11