C++/classes
Expert: Ralph McArdell - 12/14/2006
QuestionI 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 ...
-----Question-----
i was wondering how to derive a class from a vector. any suggestions?
-----Answer-----
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>
{
// ...
};
AnswerAssuming 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
Else
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_;
// ...
public:
// ...
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.