In my homework my task is to create the FooCl class for these:
double d[] = {1.3, 0.7, 2.4, 1.5, 6.2, 5.7, 8.6, 9.1};
FooCl<double> itemsD(d, sizeof(d) / sizeof(d[0]));
std::string s[] = {"C++", "Haskell", "Python", "Java"};
FooCl<std::string> itemsS(s, sizeof(s) / sizeof(s[0]));
itemsD.mySort();
itemsS.mySort();
I made a constructor/destructor for it, but I don't know how to create two different functions with templates for the two different types of lists. I think I would need to use some kind of overloading but don't know how.
template <typename T>
class FooCl
{
private:
T *mItems;
int mItemsSize;
public:
FooCl(T items[], int itemsSize)
{
mItems = new T[itemsSize];
for (int i=0; i<itemsSize; ++i)
{
this->mItems[i] = items[i];
}
this->mItemsSize = itemsSize;
};
~FooCl()
{
delete[] mItems;
}
void mySort()
{
//I have no idea how to write this function, so it can sort two different types of lists.
}
};
std::sort
?std::sort
, you will findstd::less
, for example.T
, the template parameter. I suspect that you have kind of missed the point of templates.