關鍵字
這是C++擴展運算符功能的方法,雖然樣子古怪,但也可以理解:一方面要使運算符的使用方法與其原來一緻,另一方面擴展其功能隻能通過函數的方式(c++中,“功能”都是由函數實現的)。
在symbian os c++中,返回const TUint&給描述符數據的一個指定的單個數據項。使用operator[]在描述符上循環的代價是昂貴的;替代的方法是考慮使用C++算數指針和TDesc::Ptr()。
operator在.Net中的應用,在.Net中自身的狀态機制都有現成的操作集合的範本,像需要Add +=;需要Rmove -=;讓軟件開發者在操作的時候更簡潔些,使用可重載操作的時候,更“不拘一格”;代碼如:
public class ErrorEntity
{
private IList messages=new List();
private IList codes=new List();
public static ErrorEntity operator+(ErrorEntity entity,string str)
{
entity.messages.Add(str);
return entity;
}
public static ErrorEntity operator+(ErrorEntity entity,int code)
{
entity.codes.Add(code);
return entity;
}
public static ErrorEntity operator-(ErrorEntity entity,string str)
{
entity.messages.Remove(str);
return entity;
}
public static ErrorEntity operator-(ErrorEntity entity,int code)
{
entity.codes.Remove(code);
return entity;
}
public IList Messages{get{return messages;}}
public IList Codes{get{return codes;}}
}
定義運算
可以定義運算符,讓程序比較簡潔。
來一個比較醜的高精加定義
1operator+(a,b:sz)c:sz;
2vari,ni:longint;
3begin
4fillchar(c,sizeof(c),0);
5ni:=a[0];
6ifni
7fori:=1tonido
8begin
9c[i]:=c[i]+b[i]+a[i];
10ifc[i]>9then
11begin
12inc(c[i+1],c[i]div10);
13c[i]:=c[i]mod10;
14end;
15end;
16ifc[ni+1]>0theninc(ni);
17c[0]:=ni;
18end.
單詞
名詞n.[C]
1.操作者,技工
2.司機
3.接線員
He dialed the operator.
他撥通了接線員。
4.施行手術的醫生
5.(企業)經營者
6.精明圓滑的人
7.【數】(運)算子,算符
8.旅行社計調人員