Instead of a copy
method, you should instead implement a copy constructor. It would be strange for the copy constructor to be passed an instance of itself. If you insist on checking, you can use an assertion.
template <class T>
Queue<T>::Queue(Queue<T> const & other)
: m_cap(other.m_cap), m_size(0), m_front(), m_back()
{
assert(&other != this);
enqueue(other);
}
Your copy
method is actually an assignment. It is more natural to implement an assignment operator. This can be accomplished by following the copy-swap idiom.
template <class T>
Queue<T> & Queue<T>::operator = (Queue<T> other)
{
swap(*this, other);
return *this;
}
There is also an idiomatic way to implement swap
(taught to me by Mooing Duck a long time ago):
template <class T>
class Queue {
//...
friend void swap(Queue &a, Queue &b) {
using std::swap;
swap(a.m_front, b.m_front);
swap(a.m_back, b.m_back);
swap(a.m_size, b.m_size);
swap(a.m_cap, b.m_cap);
}
};
This way, you can use argument dependent lookup (ADL) to pick the type specific swap
implementation if available. And now Queue
itself has such an implementation, which is used by the assignment operator. But it can also be used in the case Queue
is put inside an object that wants to implement swap
for itself.