You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The section correctly describes how to overload the insertion operator "operator<<" as such:
// The user defined type is passed in as a constant reference to prevent
// any unwanted modification since we are technically just printing.
ostream& operator<<(ostream& s, const YourTypes& m);
However, both the book (Listing 16-9: A Program illustrating how to implement an output operator for a vector ) and the sample code define the "operator<<" as such:
// here v is declared without the const keyword.
//This would technically allow abuses such as: See the "ABUSE" section for explanation
/*
*** ABUSE ***
assuming that the proper template type parameter constraints such as such the ability to default construct are met
*** THIS WOULD SET EVERY ELEMENT TO THE DEFAULT VALUE OF THE UNDERLINING TYPE BEFORE PRINTING. ***
for (size_t i =0; i < v.size();i++) {
v[i] = T{};
}
*/
s << "Size: " << v.size() << "\nCapacity: " << v.capacity() << "\nElements:\n";
for(const auto& element : v)
s << "\t" << element << "\n";
return s;
}
Redefining the operator as: ostream& operator<<(ostream& s, const vector<T>& v)
helps prevent this and also provide compile assistance by generating compile time errors everywhere the the variable is being modified.
suggestions: This would be a good point to emphasized in the book since theoretically speaking, and especially when dealing with large objects and/or user defined types and classes, it is recommended that the insertion operator (operator<<)
takes its second argument as a CONSTANT REFERENCE for performance and safety purposes. Stressing it in that section of the book would probably help clarify the nuance and the reason why it is recommended to passed the second argument as a constant reference.
The text was updated successfully, but these errors were encountered:
The section correctly describes how to overload the insertion operator "operator<<" as such:
// The user defined type is passed in as a constant reference to prevent
// any unwanted modification since we are technically just printing.
ostream& operator<<(ostream& s, const YourTypes& m);
However, both the book (Listing 16-9: A Program illustrating how to implement an output operator for a vector ) and the sample code define the "operator<<" as such:
// here v is declared without the const keyword.
//This would technically allow abuses such as: See the "ABUSE" section for explanation
template <typename T>
ostream& operator<<(ostream& s, vector<T> v) {
/*
*** ABUSE ***
assuming that the proper template type parameter constraints such as such the ability to default construct are met
*** THIS WOULD SET EVERY ELEMENT TO THE DEFAULT VALUE OF THE UNDERLINING TYPE BEFORE PRINTING. ***
for (size_t i =0; i < v.size();i++) {
v[i] = T{};
}
*/
s << "Size: " << v.size() << "\nCapacity: " << v.capacity() << "\nElements:\n";
for(const auto& element : v)
s << "\t" << element << "\n";
return s;
}
Redefining the operator as:
ostream& operator<<(ostream& s, const vector<T>& v)
helps prevent this and also provide compile assistance by generating compile time errors everywhere the the variable is being modified.
suggestions: This would be a good point to emphasized in the book since theoretically speaking, and especially when dealing with large objects and/or user defined types and classes, it is recommended that the insertion operator (operator<<)
takes its second argument as a CONSTANT REFERENCE for performance and safety purposes. Stressing it in that section of the book would probably help clarify the nuance and the reason why it is recommended to passed the second argument as a constant reference.
The text was updated successfully, but these errors were encountered: