Convert to a template class in a function

149 views Asked by At

I'm lamenting about a simple function that converts objects from an array-like data structure into a linked list-like one, both being ArchiCAD's own classes.

The question is that is it possible to do it for any kind of objects.

Code looks like this:

GS::Array<class T> *GetItemsFromNeig(API_Neig **p_neigs)
{
    UInt32 nSel =   BMGetHandleSize((GSHandle)p_neigs) / sizeof(API_Neig);
    GS::Array<T>*   resultArray = new GS::Array<T>;

    for (UInt32 ii = 0; ii < nSel; ++ii) {
        resultArray->Push((T) *p_neigs[ii]); //incomplete type is not allowed
    }

    return resultArray;
}

The error is not a surprise, the question is that is it possible to write a function like this.

1

There are 1 answers

0
Gyula Sámuel Karli On

For the log, the answer is:

template <class T>
GS::Array<T> *GetItemsFromNeig(API_Neig **p_neigs)
{
    UInt32 nSel =   BMGetHandleSize((GSHandle)p_neigs) / sizeof(API_Neig);
    GS::Array<T>*   resultArray = new GS::Array<T>;

    for (UInt32 ii = 0; ii < nSel; ++ii) {
        resultArray->Push((T) (*p_neigs)[ii]);
    }

    return resultArray;
}