CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Oct 2007
    Posts
    34

    [RESOLVED] How to use a derived function for std::priority_queue Compare

    I have a template class that contains a std:riority_queue and would like for the user of this class to be able to pass in the functor object that is used for comparison. Here is the code I have but it is not working. I kind of didn't think it would be thought I would give it a try. Maybe I am going about this the wrong way.

    Code:
        template <typename T>
        class ProcessingQueue {
        public:
            class Comparer
            {
            public:
                virtual bool operator()(T* obj1, T* obj2) {
                    return true;
                }
            };
    .
    .
    .
            void push(T* element);
            virtual void processElement(T* element) = 0;
    .
    .
    .
        private:
            std::priority_queue<T*, std::vector<T*>, Comparer> m_vector;
    .
    .
    .
        }
    This class creates and manages a thread that takes elements that are put on the queue and processes them. I had this working fine using a vector but want to convert it to a priority_queue to allow elements to be processed based on a user defined priority.
    My initial thought was to create the Comparer class to provide as the thrid template parameter to priority_queue and let users derive a specific Comparer for there type implementing an appropriate operator() overload. This did not work and after digging into it a little more I understand why.

    Am I going about this the wrong way?

    Can someone help with how to go about this?

    Please let me know if more information is needed. I tried to only post relevant code.

    Thanks.
    Last edited by Yadrif; September 15th, 2009 at 10:42 AM.

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

Featured