std::priority_queue derivable?

R

red floyd

I know it's generally bad from to derive from STL containers. That
said, here's my question:

I am using std::priority_queue<>. However, I need to be able to access
the underlying container; specifically, I need to be able to get at
begin() and end() for it.

Josuttis (10.3.3) shows that the compare operator and the container are
*protected* members of std::priority_queue. This would lead me to
believe that it was intended for derivation. I was thinking about doing
something like this:

class my_data { /* contents redacted */ };
class my_priority_queue : private std::priority_queue<my_data>
{
public:
typedef std::priority_queue<my_data> pq;
using pq::top;
using pq::push;
using pq::pop;
using pq::empty;
using pq::size;
my_priority_queue();
~my_priority_queue();
void do_something_to_all_elements(); // uses c.begin() and c.end()
};

Is this kosher?
 
G

Gianni Mariani

red said:
I know it's generally bad from to derive from STL containers.

It's fine to derive from STL containers.

That
said, here's my question:

I am using std::priority_queue<>. However, I need to be able to access
the underlying container; specifically, I need to be able to get at
begin() and end() for it.

Josuttis (10.3.3) shows that the compare operator and the container are
*protected* members of std::priority_queue. This would lead me to
believe that it was intended for derivation. I was thinking about doing
something like this:

class my_data { /* contents redacted */ };
class my_priority_queue : private std::priority_queue<my_data>
{
public:
typedef std::priority_queue<my_data> pq;
using pq::top;
using pq::push;
using pq::pop;
using pq::empty;
using pq::size;
my_priority_queue();
~my_priority_queue();
void do_something_to_all_elements(); // uses c.begin() and c.end()
};

Is this kosher?

In theory, this should work, however a (in)famous C++ compiler does not
accept this. (MSVC). To work around it, I redefine the methods inline
in the dervied class.
 
R

red floyd

Gianni said:
It's fine to derive from STL containers.

That



In theory, this should work, however a (in)famous C++ compiler does not
accept this. (MSVC). To work around it, I redefine the methods inline
in the dervied class.

Thanks. I guess that it's MSVC6 that doesn't like it. MSVC7.1 does,
as does g++ 3.2.2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top