You get a bonus - 1 coin for daily activity. Now you have 1 coin

3.8. Basic rules for overloading operations

Lecture



1) It is impossible to introduce your own notation for operations that do not coincide with the standard C ++ operations.

2) Not all C ++ operations can be overloaded. The following operations cannot be overloaded:

' . '- direct component selection,

' . * ' - access to the component through a pointer to it,

' ?: ' - conditional operation,

' :: ' - the operation of specifying the scope,

'sizeof',

' # ', ' ## ' - preprocessing operations.

3) Each operation specified in the language has a certain number of operands, its own priority and associativity. All these rules established for operations in the language are also preserved for its overload, i.e. you can not change them.

4) Any unary operation Å is defined in two ways: either as a component function without parameters, or as a global (possibly friendly) function with one parameter. The expression Å z means in the first case the call z.operator Å (), in the second case the call operator Å (z).

5) Any binary operation Å is also defined in two ways: either as a component function with one parameter, or as a global (possibly friendly) function with two parameters. In the first case, x Å y means calling x.operator Å (y) , in the second case - calling operator Å (x, y).

6) An overloaded operation cannot have default arguments (operands).

7) In C ++, the identity of some operations is established, for example, ++ z is the same as z + = 1 . This identity is lost to congested operations.

8) The operator function can be called by its name, for example, z = operator * (x, y) or z = x.operator * (y) . In the first case, a global function is called, in the second, a component function of class X , and x is an object of class X. However, most often the function operator is called indirectly, for example, z = x * y

9) With the exception of overloading the new and delete operations, the operator function must either be a non-static component function, or it must have at least one argument (operand) of the type “class” or “class reference” (if it is a global function).

10) The operations ' = ', ' [] ', ' -> ' can be overloaded only with the help of the non-static component function operator Е. This ensures that the first operands are left-handed expressions.

11) Operation ' [] ' is considered as binary. Let a be an object of class A in which the operation ' [] ' is overloaded. Then the expression a [i] is interpreted as a.operator [] (i) .

12) The operation ' () ' function call is considered as binary. Let a be an object of class A in which the operation ' () ' is overloaded. Then the expression a (x1, x2, x3, x4) is interpreted as a.operator () (x1, x2, x3, x4) .

13) The ' -> ' operation of accessing a class component through a pointer to an object of this class is treated as unary. Let a be an object of class A in which the ' -> ' operation is overloaded. Then the expression a–> m is interpreted as (a.operator -> ()) -> m . This means that the operator -> () function should return a pointer to class A , or an object of class A , or a reference to class A.

14) The overloading of the ' ++ ' and ' - ' operations written after the operand (z ++, z--) differs by adding the dummy parameter int into the operator function, which is used only as a sign of the difference between the z ++ and z-- operations from z and --z .

15) New global operations can be overloaded and, in general, they may not have arguments (operands) of the “class” type. As a result, it is allowed to have several global new operations, which are distinguished by changing the number and (or) types of arguments.

16) global delete operations cannot be overloaded. They can be reloaded only in relation to the class.

17) The global operations new and delete defined in the language itself can be changed, i.e. replace the version specified in the default language with your version.

18) The local functions operator new () and operator delete () are static components of the class in which they are defined, regardless of whether or not the static specifier was used (this means, in particular, that they cannot be virtual).

19) To properly release the dynamic memory for the base and derived objects, use a virtual destructor.

20) If for class X the operation “=” was not overloaded explicitly and x and y are objects of class X, then the expression x = y sets by default a byte-by-copy copy of the data of the object y to the data of the object x.

21) The operator type (operator type ) function without a return value, defined in class A , sets a type A to type conversion.

22) With the exception of the assignment operation ' = ', all operations overloaded in class X are inherited in any derived class Y.

23) Let X be the base class, Y the derived class. Then a locally overloaded operation for class X can be further reloaded in class Y.

created: 2015-12-20
updated: 2021-03-13
150



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

C ++ (C plus plus)

Terms: C ++ (C plus plus)