Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [ANSWERED] Operator Overloading with Pointers
Forum Updated to NodeBB v4.3 + New Features

[ANSWERED] Operator Overloading with Pointers

Scheduled Pinned Locked Moved C++ Gurus
9 Posts 4 Posters 10.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cazador7907
    wrote on 1 Jan 2011, 19:53 last edited by
    #1

    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 -

    Laurence -

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on 1 Jan 2011, 20:08 last edited by
      #2

      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).

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on 1 Jan 2011, 20:48 last edited by
        #3

        [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.

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Panke
          wrote on 2 Jan 2011, 09:53 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cazador7907
            wrote on 3 Jan 2011, 22:12 last edited by
            #5

            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?

            Laurence -

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on 3 Jan 2011, 22:17 last edited by
              #6

              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.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Panke
                wrote on 4 Jan 2011, 17:58 last edited by
                #7

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

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  cazador7907
                  wrote on 4 Jan 2011, 18:11 last edited by
                  #8

                  You talking above me again, mimic the STL?

                  Laurence -

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Panke
                    wrote on 4 Jan 2011, 20:06 last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0

                    1/9

                    1 Jan 2011, 19:53

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved