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 (江東敏)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市怕享,隨后出現(xiàn)的幾起案子执赡,更是在濱河造成了極大的恐慌,老刑警劉巖函筋,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件沙合,死亡現(xiàn)場離奇詭異,居然都是意外死亡跌帐,警方通過查閱死者的電腦和手機(jī)首懈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來含末,“玉大人猜拾,你說我怎么就攤上這事即舌∮逗校” “怎么了?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵顽聂,是天一觀的道長肥惭。 經(jīng)常有香客問我,道長紊搪,這世上最難降的妖魔是什么蜜葱? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮耀石,結(jié)果婚禮上牵囤,老公的妹妹穿的比我還像新娘。我一直安慰自己滞伟,他們只是感情好揭鳞,可當(dāng)我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著梆奈,像睡著了一般野崇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上亩钟,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天乓梨,我揣著相機(jī)與錄音鳖轰,去河邊找鬼。 笑死扶镀,一個胖子當(dāng)著我的面吹牛蕴侣,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播臭觉,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼睛蛛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了胧谈?” 一聲冷哼從身側(cè)響起忆肾,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎菱肖,沒想到半個月后客冈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡稳强,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年场仲,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片退疫。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡渠缕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出褒繁,到底是詐尸還是另有隱情亦鳞,我是刑警寧澤,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布棒坏,位于F島的核電站燕差,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏坝冕。R本人自食惡果不足惜徒探,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望喂窟。 院中可真熱鬧测暗,春花似錦、人聲如沸磨澡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽钱贯。三九已至挫掏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間秩命,已是汗流浹背尉共。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工褒傅, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人袄友。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓殿托,卻偏偏與公主長得像,于是被迫代替她去往敵國和親剧蚣。 傳聞我的和親對象是個殘疾皇子支竹,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容