0

I overloaded << and >> with friend function but Can we do it without using friend function?

east1000
  • 1,240
  • 1
  • 10
  • 30
  • Yes. Example: https://godbolt.org/z/G6faWEdfP You can make it a friend function to access private members too but it's not necessary. – mediocrevegetable1 Sep 16 '21 at 03:06
  • Yes. The friend part is just to allow it accesses to private members (and document that it is tightly bound to the implementation). If you don't need to use private members (ie just use the public interface), then it is not tightly bound and thus does not need to be a friend. – Martin York Sep 16 '21 at 03:33
  • It is common to implement them as non-friends that call public virtual `write` and `read` functions. This gives you both a "normal" function interface and "polymorphic" operators that behave as you expect. – molbdnilo Sep 16 '21 at 08:03

0 Answers0