I have seen a few examples of abstract classes with a friend operator<<
and a virtual "print
" member function where the two declarations are both in the protected
section. For example:
class Function{
public:
virtual ~Function() {}
virtual double value(const double x) const = 0;
virtual Function* clone() const = 0;
protected:
friend ostream& operator<<(ostream& os, Function& f);
virtual void print(ostream& os) const = 0;
};
ostream& operator<<(ostream& os, Function& f){
f.print(os);
return os;
}
I do not understand why this is mandatory. Can someone explain?
Thanks!