c++ - std::vect sorting with member variable -
i'm stuck piece of code:
class myobject { public: int value; } class myclass { private: btalignedobjectarray<myobject*> m_objects; public: int comp (myobject *a, myobject *b) { return calculatethenewvalue(a->value) < calculatethenewvalue(b->value); } void dosort() { m_objects.quicksort(comp); } //edit: member function needed sorting int calculatethenewvalue(int v) { // calculation using other members variables, not m_objects } };
it doesn't compile because comp non static member function.
comp cant static, because needs access member variable m_objects.
also defeat encapsulation of m_objects have static function , call this
myclass::dosort(myclass.m_objects)
edit
this declaration of btalignedobjectarray
http://bulletphysics.org/bullet/bulletfull/btalignedobjectarray_8h_source.html
line 365 has declaration or quicksort
if need make comp
binary function, wrap in functor. if can use c++11, use lambda:
m_objects.quicksort([&](myobject * lhs, myobject * rhs) { return this->comp(lhs,rhs) });
if can't use c++11, make functor class similar behavior.
struct compare { myobject & obj_; compare(myobject& obj) :obj_(obj) {} bool operator()(myobject * lhs, myobject * rhs) const { return obj_.comp(lhs,rhs); } }; ... void dosort() { m_objects.quicksort(compare(*this)); }
Comments
Post a Comment