A Little discussion about "C++ Operator "

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://upload-images.jianshu.io/upload_images/1868901-ffcb86ebb4ff989d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Table2:(Increment/decrement operators)


![image.png](http://upload-images.jianshu.io/upload_images/1868901-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 (江东敏)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,012评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,628评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,653评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,485评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,574评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,590评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,596评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,340评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,794评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,102评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,276评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,940评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,583评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,201评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,441评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,173评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,136评论 2 352

推荐阅读更多精彩内容