I was also wondering how to write a peek function for a queue, that will return the string at the front of my queue, but not remove it?
The text above is a follow-up to ...

i was wondering how to derive a class from a vector.  any suggestions?
Yes. In short donít.

The C++ standard library containers are _not_ designed to be derived from. Doing so is very poor style.

For a start they have non virtual public destructors. If a pointer to a std::vector class template specialisation were used to delete an instance of your super_vector derivation (or super_vector specialisation if it is a class template) your super_vector destructor will _not_ be called. I use the term specialisation to refer to an actual class formed from a class template such as std::vector by providing the template with template arguments.

That is a specific specialisation of std::vector such as std::vector<int> is a concrete class.

Instead use composition (a.k.a. containment, layering or embedding): develop classes that have std::vector specialisations as data members:

class A
   std::vector<int> values_;

// ...


You could also develop class templates that pass some or all of their template arguments on to their std::vector data members:

template <typename T, class Allocator=std::allocator<T> >
class G
   std::vector<T, Allocator> values_;

// ...


Note that I have provided the G class template above with the _same_ template parameters as std::vector, with the same default value for the Allocator parameter.

However if you really, really, really have to derive from std::vector (i.e. you have _no_ other choice or you really know what you are doing) you can of course do so, but on your own head be it:

class AD : public std::vector<int>

// ...


Or even:

template <typename T>
class GD : public std::vector<T>

// ...


Assuming you are using the C++ standard library, which is likely judging from  your previous question, why bother? The standard C++ container adapter std::queue functions in this fashion anyway. The operation is called front() Ė it returns the next element in the queue without removing it. To remove an element from the queue you use the pop() operation. To add an element to the queue you use push().

Exactly how to write such a peek function would depend on the implementation of your queue, but the pseudo code would look something like:

   string_queue::peek() : returns reference to string
   Begin Function

       If the queue is empty
         Then throw EmptyQueue exception
         Return the next queue item
       End If
   End Function

In reality you would probably need to have two such functions. The second would be a const member function returning a reference to const string.

Returning the next queue item does not cause it to be removed. Adding another item to the queue adds it to the other end of the queue meaning that calling peek before and after items are added to the queue will reference the same queue item.

For example if your queue were based on a doubly linked list of some sort then the next queue item would be the value held by the last (or back) list node and new queue items would be added to the front list node.

To obtain the last item without removing it you would use the back-link of the list (itís doubly linked remember) to access the last node and return a reference to that nodeís data item.

To remove an item from the queue (e.g. via a separate pop operation), without accessing its value, you would simply delete the last list node.

Note that by default a std::queue uses a std::deque (pronounced "deck") as its wrapped container type. A deque is a structure for which it is efficient to insert and delete elements at _both_ ends of the collection. This differs from a vector for which it is only efficient to insert and delete items from the end (back) of the collection.

You might like to examine the implementation of the std::queue implementation for the standard C++ library implementation you are using. You should start with the queue header. You may have to follow the #include directives and examine other headers as the details may be in some other file included directly or indirectly by queue. You will probably see something like this:

   template < class T,   class ContainerT = deque<T> >
   class queue

   // ...

  ContainerT container_;

   // ...


   // ...

       reference front()
         return container_.front();

       const_reference front() const
         return container_.front();

   // ...


Where container_ is a member of the type of the wrapped container (ContainerT template parameter) used to implement the queue (std::deque<T> by default) and reference and const_reference are typedefs (type aliases) for reference to the queued type as non-constant and constant.

As you can see this really is a wrapper class (or rather class template). All it does is hand the work off to the container member, including handling accesses to an empty container. You will note that std::queue uses the composition style recommended in the answer to your previous question.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2017 About.com. All rights reserved.