日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Operator Overloading

發布時間:2025/6/17 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Operator Overloading 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.重載一元操作符

To declare a unary operator function as a nonstatic member, you must declare it in the form:

ret-type?operatorop()

where ret-type is the return type and op is one of the operators listed in the preceding table.

To declare a unary operator function as a global function, you must declare it in the form:

ret-type?operatorop(?arg?)

where ret-type and op are as described for member operator functions and the arg is an argument of class type on which to operate.

?

There is no restriction on the return types of the unary operators. For example, it makes sense for logical NOT (!) to return an integral value, but this is not enforced.

?

2.重載二元操作符

To declare a binary operator function as a nonstatic member, you must declare it in the form:

ret-type?operatorop(?arg?)

where ret-type is the return type, op is one of the operators listed in the preceding table, and arg is an argument of any type.

To declare a binary operator function as a global function, you must declare it in the form:

ret-type?operatorop(?arg1,?arg2?)

where ret-type and op are as described for member operator functions and arg1 and arg2 are arguments. At least one of the arguments must be of class type.

?

There is no restriction on the return types of the binary operators; however, most user-defined binary operators return either a class type or a reference to a class type.

?

3.operator=

The assignment operator (=) is, strictly speaking, a binary operator. Its declaration is identical to any other binary operator, with the following exceptions:

  • It must be a nonstatic member function. No operator= can be declared as a nonmember function.

  • It is not inherited by derived classes.

  • A default operator= function can be generated by the compiler for class types if none exists.

?

4.operator()

實現Function object

?

5.operator[]

The subscript operator must be a nonstatic member function that takes a single argument. This argument can be of any type and designates the desired array subscript.

?

6.operator->

Class member access can be controlled by overloading the member access operator (–>). This operator is considered a unary operator in this usage, and the overloaded operator function must be a class member function. Therefore, the declaration for such a function is:

class-type *operator–>()

where class-type is the name of the class to which this operator belongs. The member access operator function must be a nonstatic member function.

This operator is used (often in conjunction with the pointer-dereference operator) to implement "smart pointers" that validate pointers prior to dereference or count usage.

The . member access operator cannot be overloaded.

?

7.Increment and Decrement

?

8.Conversion Functions

conversion-function-name:

operator conversion-type-name ()

conversion-type-name:

type-specifier-list ptr-operatoropt

Conversion functions are often called "cast operators" because they (along with constructors) are the functions called when a cast is used.

Conversion functions are inherited in derived classes. Conversion operators hide only base-class conversion operators that convert to exactly the same type. Therefore, a user-defined operator int function does not hide a user-defined operator short function in a base class.

Only one user-defined conversion function is applied when performing implicit conversions. If there is no explicitly defined conversion function, the compiler does not look for intermediate types into which an object can be converted.

If a conversion is required that causes an ambiguity, an error is generated. Ambiguities arise when more than one user-defined conversion is available or when a user-defined conversion and a built-in conversion exist.

// spec1_conversion_functions2.cpp #include <string.h> #include <stdio.h>struct String {// Define constructor that converts from type char *.String( char *szBuffer ) {strcpy_s( _text, szBuffer );}// Define conversion to type char *.operator char *() {return _text;}int operator==( const String &s ) {return !strcmp( _text, s._text );}private:char _text[80]; };int main() {String s( "abcd\0" );char *ch = "efgh";// Cause the compiler to select a conversion.return s == ch; // C2666 }

In the expression s == ch, the compiler has two choices and no way of determining which is correct. It can convert ch to an object of type String using the constructor and then perform the comparison using the user-defined operator==. Or it can convert s to a pointer of type char * using the conversion function and then perform a comparison of the pointers.

Because neither choice is "more correct" than the other, the compiler cannot determine the meaning of the comparison expression, and it generates an error.

注:若在構造函數前加上explicit就不存在歧義了

轉載于:https://www.cnblogs.com/avexer/archive/2012/11/05/3258318.html

總結

以上是生活随笔為你收集整理的Operator Overloading的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日日插插 | 久久福利影院 | 中文字幕在线观看精品 | 国产不卡精品视频 | 久久黄色免费视频 | 呦呦精品| 成年人黄色免费网站 | 日本视频中文字幕 | 韩国伦理电影免费在线 | 天天曰夜夜曰 | √8天堂资源地址中文在线 欧美精品在线一区二区 | 日韩黄色大全 | 日日嗨av一区二区三区四区 | 国产精品自拍第一页 | 日韩免费久久 | 特黄做受又粗又大又硬老头 | 国产成人综合一区二区三区 | 主人性调教le百合sm | 亚洲精品一区二区三区不卡 | 国产又粗又猛又爽视频 | 日韩精品一区二区三区视频在线观看 | 色哟哟精品一区 | 日本青青草视频 | 国产成人啪精品午夜在线观看 | 在线免费观看视频 | 日韩在线观看av | 操人视频免费看 | 国产69精品久久久久久久久久 | 国产日韩精品在线 | 国产精品久线在线观看 | 中文字幕一区二区三区免费视频 | 中文字幕在线观看网址 | 日操夜操天天操 | 日本乱偷人妻中文字幕在线 | 午夜在线看| 国产精品午夜在线 | 人人妻人人爽人人澡人人精品 | 俄罗斯精品一区二区三区 | 天天干天 | 国产黄a三级三级三级看三级男男 | 永久免费不卡在线观看黄网站 | 日韩污视频在线观看 | 色小姐在线视频 | 成人午夜免费在线 | yes4444视频在线观看 | 亚洲无人区码一码二码三码的含义 | 国产精品一区麻豆 | 黄色视屏免费 | 丝袜 中出 制服 人妻 美腿 | av手机观看 | 亚洲视频日韩 | 中国毛片在线 | 亚洲天堂美女 | 人妻在线日韩免费视频 | 红桃视频国产 | 韩日欧美| 高清av网| 亚洲视频日韩 | 调教在线观看 | 在线观看成人免费视频 | 老熟妇一区二区三区 | 大地资源在线观看免费高清版粤语 | 九九九亚洲 | 日韩欧美精品在线 | 精品在线免费观看 | 日批视频网站 | 激情视频激情小说 | 精品国产无码在线观看 | 亚洲88av| 意大利少妇愉情理伦片 | 桃色成人网 | 综合久久99 | 蜜臀av在线观看 | 欧美丰满少妇人妻精品 | 欧美成人一区二区三区片免费 | 亚洲成人经典 | 青青青草视频在线 | 国产夫妻精品 | 美女网站在线免费观看 | 亚洲激情六月 | 天堂av官网 | 五月婷婷久久久 | 国产精品久久久久久久久免费相片 | 成人免费自拍视频 | 久久免费黄色 | 欧美视频免费 | 国产精品白丝喷水在线观看 | 久久综合亚洲色hezyo国产 | 日日夜夜免费精品 | 国产精品白浆一区二小说 | 天天操天天爽天天干 | 日韩免费在线观看视频 | 精品一区二区三区四区 | 国产精品无码免费在线观看 | 亚洲高清免费 | 成人精品视频在线观看 | 亚洲精品一区二区三区新线路 | aaa级黄色片 | 丝袜黄色片 |