Best Practices with C++ ostream (tostring) -
what best practices when overloading <<
operator. particularly, how distinguish between opearting on pointer vs object. kosher them both output same string when fed <<
?
for instance, consider following code both book
objects have been initialized
book b1; book* b2; // initialization stuff // can both of these output same representation of book object? cout << b1 << endl; cout << b2 << endl;
how distinguish between opearting on pointer vs object.
by signature of operator<<
function:
std::ostream& operator<<(std::ostream&, const book&); // operates on object std::ostream& operator<<(std::ostream&, const book*); // operates on pointer
is kosher them both output same string?
it allowed, not particularly useful. rare see second form implemented. if tempted implement second form, realize entirely redundant. example, if have book* pbook
:
std::cout << *pbook << "\n";
that print object pbook
points quite nicely.
Comments
Post a Comment