C ++ overload operator

xiaoxiao2021-03-06  65

When an overload operator is a function of a name space, the first and second parameters of the operator, that is, the left and right two operands of the operator are considered.

What should I generally decide whether to declare a operator as a member or a name space member? In some cases, the programmer has no room: 1 If a heavy load operator is a class member, then only when it is used with it When the left operation is the object of the class, it will be called. If the left operating number of the operator must be other types, then the overload operator must be a member of the name space. 2 C requirements assignment = subscript [ ] Call () and members Access Arrows -> Operators must be defined as class member operators. Any definitions defined for these operators to name space will be marked as compilation time errors. For example: // Error: must be class Member Char & Operator [] (String &, IX); The situation, such as a symmetric operator, such as equal to the operator, is best defined as a namespace member.

转载请注明原文地址:https://www.9cbs.com/read-119881.html

New Post(0)