What is friend ostream

前端 未结 4 551
北海茫月
北海茫月 2021-01-03 02:24

I\'m a bit confused about what exactly this line of code means in my header file.

friend ostream & operator << (ostream &, const something &         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-03 03:02

    That line of code says that the operator << is a friend of something (since it's listed in the something class definition). This means that that operator << function can access the variables inside there.

    The & here as the parameters mean that you pass in objects when calling the method and those parameters will just be another name for those parameter objects. Returning ostream & means that you're going to return the ostream parameter so that you can connect << expressions together avoiding creating a new "cout" when using the one global cout is what is needed.

提交回复
热议问题