Archived

This topic is now archived and is closed to further replies.

Roof Top Pew Wee

More advanced template stuff. . I think advanced at least.

Recommended Posts

Let''s say I have a teamplate and I want to add a sort function. The problem is that with a template, you never know the variable type that the template is going to be for. Well, my specific template is made for a few of my classes, and each class will be sorted by a different variable. So here''s a simplified version:

template <class cType>  class dArray
{
  cType arrayVariable[64];
  void sortByVariable( /*What do I put here?*/);


}

template <class cType> void dArray<cType>::sortByVariable(arg)
{

}

One of my classes has a variable float z, that I want to sort by, and I will be making one dArray of this class. The other class has a variable that is float time, and that is what I want to sort this one by. Any way I could do this? --Vic-- The future of 2D game development: Flat Red Ball

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Unless you really want to define your own class (as an exercise... or I dont know why), I advise you to use STL containers.

Example :
class Z_Ordered
{
public:
Z_Ordered(double z_) : z(z_) {}
double z;
};

bool less(const Z_Ordered& a, const Z_Ordered& b) { return a.z < b.z; }

int main()
{
std::vector<Z_Ordered> v;
v.push_back(1.);
v.push_back(0.5);
std::sort(v.begin(), v.end(), less);
}

Share this post


Link to post
Share on other sites