You can use a function similar to this in your class:
void ListType::output(std::ostream& out) const {
for (int i = 0; i < count; i++) {
if (i > 0) { // No comma for first element
out << ", ";
}
out << items[i];
}
}
The overloaded <<
method of ostream
can then be rewritten to this to call the output function:
std::ostream& operator << (std::ostream& out, const ListType& my_list) {
my_list.output(out);
return out;
}