say I have classes: Square Circle etc. , all inheriting from class Shape, each has a draw Method. ( Shape::draw() does nothing)
I made a Template of a dynamic array, as such:
template <class T>
Array<T>::Array()
{
cap = 8; // First allocation
len = 0;
array = new T[cap];
}
template <class T>
T* Array<T>::operator [] (unsigned int index)
{
return &array[index]; // return array element
}
template <class T>
void Array<T>::add(const T &item)
{
len++;
if (len > cap)
{
cap *= 2;
array = (T *)realloc(array, sizeof(T) * cap);
}
array[len - 1] = item;
}
when I run this simple code on main:
Shape *s;
OrthogonalTriangle t = OrthogonalTriangle( 12);
s = &t;
s->draw();
Array<Shape> A;
A.add( t );
A[0]->draw();
well, first draw works, and draws the triangle. second one just calls Shape::draw()
I tried almost evrything inclouding changing the template entirely and returning pointers, or by ref...