Copyright @ Joel Jiang(江东敏) at 2017.07.21 13:00 p.m
in Shenzhen.China. LIST- TE- E11 -02
Operator is a key word in c + + that is used together with operators ,some calculation symbol, to represent an Operator function, which should be considered as a function name in general. And this is the way that we can stretch operator ,although it is odd, but it can be perceive: On the one hand, you want to make the operation of the operator consistent with the original method, on the other hand, the function can only be extended through the function (in c + + language, the function is implemented by the function).
**1:Operator overloading is implemented as class member function **
Overloaded operators are declared in the class, a declarative method as the same as the ordinary member function, but his name contains the keyword of operator, except followed by a c + + predefined operators .
The left operator has a hidden default * * this * * instead , in operator overloading, this can't be written in the parameter brackets, only can be written in the function, or not written of all.
Declaring a predefined that "= = operator" may in the beneath way:
complex::operator += (const complex& i_member)
{
return __doapl(this, i_member)
}
2:Operator overloading is implemented as not a class member function(global function)
For the global overload operator, the parameter of left operator means it have to explicitly specify. Such as:
#include
using namespace std;
class person
{
public:
int age;
public:
};
bool operator==(person const &p1 ,person const & p2)```
To meet the requirements, the type of _ operation _ is shown as specified:
{
if(p1.age==p2.age)
return true;
return false;
}
int main()
{
person rose;
person jack;
rose.age=18;
jack.age=23;
if(rose==jack)
cout<<"ok"< return 0;
}
**3:How do you decide whether to load an operator into a class member function or a member of the global namespace?**
* If an overloaded operator is a class member, then only the left operands that are used with him are the object of the class, which is called. If the left operand of the operator must be another type, the operator must be reloaded as a member of the global namespace.
* c + + requires assignment ** = **, the subscript ** [] , **call **(), ** and members to the "- > operator" must be defined as a class member operators. Any definition that defines these operators as namespace members is marked as compile time errors.
* If an operand is a class type such as a **string** class, it is best defined as a global namespace member for symmetric operators such as operators.
**4:Overload operators have the following limitations:**
1. Only some in predefined operator gathering can be overloaded.
2. For the built -in type operator, it predefined do not be change ,should not be overloaded.E,g ,we can change the"int" type operator meaning.
3. Cannot define other operators for built-in(default data types) data types;
4. Only operators that can override class types or enumerated types;
5. Overloaded operators cannot change their operator precedence;
6. Overload operation isn't change the number of the operator.
7. Apart from the operator **"()"**,others supply default argument for overload operator are illegal.
**5: Some notices**
1.If you want to determine which left return value or right return value, there is a method that return reference if it is a left value , otherwise return the value if it is right value.
2.Such as "**+** "operator value after no object can accommodate to be change, for the best return values, otherwise only to operator create a temporary object that used to accommodate changes in the object body after the value, If in the heap create a temporary object and returns a pointer or reference, outside the operator function also need to release it, and if the returned object rather than a reference or pointer, we can test the efficiency is low. If the return value is data, the best way is in the constructor of the class add the types of data conversion function, such as returning the value is an **int **type, so it is better for a ** int** type as a parameter of the constructor.
3.Place the the parameter of "int" type in the operator increment. If it is the return value ,for the no parameter in
pre-increment ,return reference, e.g:
class Test
{
public:
Test(x=3){ m_value = x}
Test &operator ++(); //the obj plus before (++~)
Test &operator ++(int);//the obj plus after (~++)
private:
Int m_value:
};
Test &Test::operator ++()
{
m_value ++; //the obj plus before (++~)
return *this; // return the temporary obj
}
Test Test::operator ++(int)
{
Test tmp(*this); // create the temporary obj
m_value ++; // the obj plus after (~++)
return temp; // return the temporary obj
}
4.Due to the cast for the basic data types, so must define it(cast conversion) by yourself.
5.The format of cast overload conversion :**operator** type name (); it is no return value, because its type name is present its return type,that is a no significative thing waste the time.
6.In general, the conversion operator is reciprocal with the transformation constructor (that is, a constructor with a parameter), and if you have a constructor ** Test (int) **, it's best to have a conversion operator **int ()**. This does not have to provide an object parameter overload operator, such as **Test a1 (1)**; The Test ** a2 (2); ** the **Test a3; A3 = a1 + a2 **; Don't need to overload ** +** operator, because for the operation of a1 + a2, system may find whether you define first **+ ** for the Test operators, if not, it will look for any transformation function of type Test for parameter types of the + operator (because no. **+ ** can be the type of operation result by transforming the function conversion as the Test object), because the Test class have an int type parameters, have the + operator to type int, so ** a1 + a2** real execution is ** Test (int (a1) + int (a2)) **. **Test (3)**
7.For the conversion operator, there is one more thing to notice, that is if the class of A have conversion operator(constructor function) which include the parameter of B, then, the class of B doesn't have the conversion operator that the class of A. Otherwise, there is the ambiguity of transformation. e.g.
```class A{A(B&){…}}; class B{ operator A(){…}};```
Then, this sentences have some problem:
```B b; A(b);//A(b)```
// some is may the constructor of A, maybe is a conversion operator.
8.Implicit conversion
C++ can use **operator** to accomplish overload implicit conversation operator. The format is as follows:
operator T (),T is a type, such as:
class A
{
public:
operator B() { return this->b_; }
operator const B() { return this->b_; }
operator B&() { return this->b_; }
private:
B b_;
};
A a;
If(a),It is change to the form of "(a.operator B*())" when it is compiled ,and virtually is resemble as if(a.b_).
***
The last reference :C++ operators Table1 : (C++ operators are evaluated in the following order)
![image.png](http:https://img.haomeiwen.com/i1868901/ffcb86ebb4ff989d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Table2:(Increment/decrement operators)
![image.png](https://img.haomeiwen.com/i1868901/fee19a2a1caa72ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Regarding of the explanation atop,can look up the c + + official documents by many explaining.
***
The sharing of knowledge, the spirit of encouragement.
By Joel Jiang (江东敏)
网友评论