I'm reading Scott Meyers' "Effective C++, 3rd edition". I'm still with item 19, but I followed some cross references and ended up in item 46: "Define non-member functions inside templates when type...
Type: Posts; User: comocomocomo
I'm reading Scott Meyers' "Effective C++, 3rd edition". I'm still with item 19, but I followed some cross references and ended up in item 46: "Define non-member functions inside templates when type...
You are right. I included inheritance in an attempt to address the most general case.
When using inheritance, you just need to write the operator<< once. I find this fact very interesting. That's...
Oh, and you might want B's print() to show the inherited part of the object by calling A's print():
class B : public A
{
public:
virtual void print (std::ostream & os) const
...
I think it's the other way around. If everything is passed by reference, my design requires no copy operations. If you put the text in a std::string and then pass it to the op<< for strings, you...
In these cases I prefer to implement the functionality in a public method (virtual, most of the times). The operator << receives a reference to the object and calls the virtual method.
Very interesting! Well, that narrows my list of real-life, good 'friend' examples (from my own experience) to... zero ;-)
Suppose that you have to write your own STL-like container. The iterators might need to access the internals of the data structure. When you implement the ++ operator, for instance, you have two...