[ANSWERED] Operator Overloading with Pointers



  • Just when I think I'm starting to understand things in the C++ universe ....

    Created a simple template binary heap (I was curious about how they work). The heap works brilliantly as long as I use simple types (int, double, float, etc.). However, when I attempted to use it with pointers to a series of class objects, I found that the operator overloading that I had in place for the class objects didn't work. After some research, I realized that the problem rested in the fact that the heap is storing pointers and not the class object itself.

    This works ....
    @
    TestClass* t1 = new TestClass("Five", 5);
    TestClass* t2 = new TestClass("Two", 2);
    TestClass* t3 = new TestClass("seven", 7);

    if( *t1 > *t2 )
        qDebug() << "t1 is greater than t2";
    else
        qDebug() << "t1 is less than t2";
    
    if( *t1 > *t3 )
        qDebug() << "t1 is greater than t3";
    else
        qDebug() << "t1 is less than t3";
    

    @

    This does not
    @
    TestClass* t1 = new TestClass("Five", 5);
    TestClass* t2 = new TestClass("Two", 2);
    TestClass* t3 = new TestClass("seven", 7);

    if( t1 > t2 )
        qDebug() << "t1 is greater than t2";
    else
        qDebug() << "t1 is less than t2";
    
    if( t1 > t3 )
        qDebug() << "t1 is greater than t3";
    else
        qDebug() << "t1 is less than t";
    

    @

    So, my question is how do I set up the operator overloading (or the heap's pop and push functions) such that I can run the comparison for either simple types or class objects? I think that if I only stored pointers (regardless of the type of information being pointed to) in the heap, then this would not be a problem. I would like to think that there is a more "elegant" solution.

    I hope everyone had a great Christmas and New Years!!

    Laurence -



  • Hi Laurence,

    Your problem is C++ related. A pointer is an address in memory. If you store pointers in your heap, and compare them as you described, you compare the adresses. If you want to compare the objects, you need to dereference the pointers (as you did in working example).

    If you want your heap to be able to work with simple types and classes, you have to store objects, not pointers. If this has influence on the data (as it must then be copied) you could use shared data (QSharedData) inside your classes to have the same logic as the Qt data classes.

    You can't setup operator overloading for pointers. It's not possible. A pointer is a simple type (adress of memory).



  • [quote author="cazador7907" date="1293911612"]
    So, my question is how do I set up the operator overloading (or the heap's pop and push functions) such that I can run the comparison for either simple types or class objects?
    [/quote]

    You can't change the implementation of operators for C++ builtin types. operator<(void*, void*) is already defined by the language.



  • You could make a specialized heap
    @
    template<typename T*>
    class BinaryHeap { ... }
    @
    where you compare *item1 < *item2 instead of item1 < item2.

    I don't think, thats makes sense though. You would create "random" behaviour for the enduser
    of this class. A better solution may be

    @
    template<typename T, typename pred>
    class BinaryHeap {
    ...
    pred(item1, item2)
    ...
    }
    @

    Where you use the functor pred to define a order on the elements.



  • I'm a little on the slow side when it comes to template as you described above.

    Are you referring to creating a specialized template for the heap?



  • yes, one template for types and one for pointers.

    Or one with a comparison function as second template argument (functor pred), so you can use one template implementation, which could have a default pred that compares the types directly.



  • Yes and please use the second version, to mimic the STL. Everything else would be confusing :-)



  • You talking above me again, mimic the STL?



  • Sorry, if I wasn't clear. I made two suggestions to solve your problem and the longer i think about the first one the less I like my own solution. Because it would work in another way than the standard library and thus may confuse readers of your code.

    I as an user of your binary heap would expect that the implementations calls operator<(T, T) for all possible T in the same way.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.