Why define operator + or += outside a class, and how to do it properly?

前端 未结 3 1629
故里飘歌
故里飘歌 2020-12-04 15:47

I am a bit confused about the differences between

Type  operator +  (const Type &type);
Type &operator += (const Type &type);

a

3条回答
  •  爱一瞬间的悲伤
    2020-12-04 16:18

    The friend specifier is used when the thing being declared isn't a member of the class, but needs access to the private members of the class's instances in order to do its job.

    If your operator will be defined in the class itself, use the first way; if it'll be a standalone function, use the second.

提交回复
热议问题