Results 1 to 4 of 4

Thread: Need to get around some pointers

  1. #1

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    Need to get around some pointers

    I have a template class that implements a PriorityQueue, the important thing about the PQ is that it requires the < operator and the == operator of the typename that I give it.

    So it works great with ints, PriorityQueue<int> is great. But if I use PriorityQueue<int*> i have a problem because it doesn't deference the pointer when doing the comparisons on it. I'm guessing it just compares the pointer address or something.

    How can I get it to derefence if the typename if its a pointer and not if its not a pointer. I need this for passing a class as the typename that overloads the < and == ops.

    NOMAD

  2. #2
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    use a try...catch? or make a bool var that you set true if it is a pointer and false if its not? or would it be possible to make new operators? <*, ==*? (I forget if its possible to define your own operators)
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by DNA7433
    use a try...catch? or make a bool var that you set true if it is a pointer and false if its not? or would it be possible to make new operators? <*, ==*? (I forget if its possible to define your own operators)
    Its not.

    The easy way is to write a container struct:
    Code:
    struct _that_holds_int_pointers
    {
    public:
     bool operator ==(_that_holds_int_pointers& rhs);
     bool operator <...;
     int* member;
    };
    Override those operators, and compare the *member value.

    The STL priority_queue takes as one of its template parameters the typename of a comparison object which does the comparisons between members. Using this method, one could simply write a new comparison object to use.

    Z.

  4. #4

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237
    Thanks guys I'll try that

    NOMAD

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width