Jump to content
  • Advertisement

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.

This topic is 5497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!