how do I distinguish between opearting on a pointer vs an object.
By the signature of the operator<<
function:
std::ostream& operator<<(std::ostream&, const Book&); // operates on object
std::ostream& operator<<(std::ostream&, const Book*); // operates on pointer
Is it kosher for them to both output the same string?
It is allowed, but not particularly useful. It is very rare to see the second form implemented. If you are tempted to implement the second form, realize that it is entirely redundant. For example, if you have a Book* pBook
:
std::cout << *pBook << "\n";
That will print the object to which pBook
points quite nicely.