Lecture
Any unary operation Å can be defined in two ways: either as a component function without parameters, or as a global (possibly friendly) function with one parameter. In the first case, the expression Å Z means a call to Z.operatorÅ (), in the second case, a call to operatorÅ (Z).
· Unary operations that are overloaded within a certain class can only be overloaded via a non-static component function without parameters. The called class object is automatically perceived as an operand.
· Unary operations that are overloaded outside the scope of a class (as global functions) must have one class type parameter. The object transmitted through this parameter is perceived as an operand.
Syntax:
a) in the first case (description in the class area):
return_type operator
b) in the second case (the description is outside the scope of the class):
operator_type_of_operations operator_array_type (type_id)
Examples
1) class person 2) class person
{int age; {int age;
... ...
public: public:
... ...
void operator ++ () {++ age;} friend void operator ++ (person &);
}; };
void main () void person :: operator ++ (person & ob)
{class person jon; {++ ob.age;}
++ jon;} void main ()
{class person jon;
++ jon;}
Comments
To leave a comment
C ++ (C plus plus)
Terms: C ++ (C plus plus)