1

I'm stuck with this 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: this member function is needed to do the sorting
    int calculateTheNewValue(int v)
    {
            // do some calculation using other members variables, not necessarily m_objects
    }

};

It doesn't compile because comp is a non static member function.

comp cant be static, because it needs to access the member variable m_objects.

Also it would defeat the encapsulation of m_objects to have a static function and call it like this

MyClass::doSort(myClass.m_objects)

Edit

This is the declaration of btAlignedObjectArray

http://bulletphysics.org/Bullet/BulletFull/btAlignedObjectArray_8h_source.html

Line 365 has the declaration or quicksort

rraallvv
  • 2,875
  • 6
  • 30
  • 67
  • comp, as it is, doesn't require any access to m_objects, because you already pass the objects that should be compared with each other. Are you sure the example is complete? – j_schultz Aug 13 '13 at 01:34
  • @j_schultz sorry, I omitted an important part of code, maybe too much (or too little) coffee. calculateTheNewValue() is a member function needed to do the sorting – rraallvv Aug 13 '13 at 01:49
  • Show declaration of `btAlignedObjectArray::quickSort`. – awesoon Aug 13 '13 at 02:17
  • comp and calculateTheNewValue can be static if calculateTheNewValue do not reference m_objects. – lulyon Aug 13 '13 at 02:24
  • @lulyon calculateTheNewValue calculates a value referencing other member variable, I have to edit to clarify – rraallvv Aug 13 '13 at 02:26

1 Answers1

2

If you need to make comp into a binary function, then wrap it in a functor. If you can use C++11, then use a lambda:

m_objects.quickSort([&](MyObject * lhs, MyObject * rhs) {
        return this->comp(lhs,rhs)
    });

If you can't use C++11, then make a functor class with 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));
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • I'm using Xcode 4.6, it can't recognise the [&](x,y){ ...} semantic, I'll try the functor approach – rraallvv Aug 13 '13 at 02:44
  • btw, how is it called, it is kind of a block of code being passed as a parameter – rraallvv Aug 13 '13 at 02:46
  • @rraallvv: You mean what is that syntax called? It's called a [lambda](http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B). – Benjamin Lindley Aug 13 '13 at 02:50
  • @rraallvv: By the way, I don't use XCode, but I'm pretty sure it supports C++11, you just need to enable it. Check this: http://stackoverflow.com/questions/4574246/can-i-use-c11-with-xcode – Benjamin Lindley Aug 13 '13 at 02:52
  • the lambda approach worked, Xcode shows some other errors in other files but I can try to enable c++11 only for this one – rraallvv Aug 13 '13 at 02:59