global class

Hi!

I created a method which deletes a pointer from a array of pointers and then moves all the subsequent pointers one position to the left. This is the code I wrote:

1
2
3
4
5
6
7
8
9
10
11
void EU::deleteElement(Dogs *ar[],int index, int length){
//ar: array of pointers
//index: index of the element to be removed
//length: length of the array

	for (int i = index; i <length-1; i++){
		ar[i] = ar[i+1]; 
        }

}


My problem is that now I want to do the same with an array of pointers which do not point to Dog objects but to different objects, let's say Computers. Is it any way to create in C++ a function wich works with all the different classes:

 
void EU::deleteElement(GLOBAL_CLASS *ar[],int index, int length){


or my only solution is to create the same method but for the class Computers.

Thank you in advanced
Look up templates. The function would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

template <class T>

void EU< T >::deleteElement( T *ar[], int index, int length )
    {
    for( int i = index; i < length - 1; i++ )
        {
        ar[ i ] = ar[ i + 1 ];

        }    /*    for( i < length - 1 )    */

    }
        /*    deleteElement()    */


NOTE: I have not tried to compile this: YMMV ...
Yes, that was what I was looking for! The correct code is:

1
2
3
4
5
6
7
template <class T> void EU::deleteElement(T *ar[],int index, int length){

	ar[index]=0; 
	for (int i = index; i <length-1; i++){
		ar[i] = ar[i+1]; 
	}
}


Then, it must be defined in the .h file as:

template <class T> void deleteElement(T *ar[],int index, int length);

Thanks :)
You're welcome! Templates are our friends! ;)
Topic archived. No new replies allowed.