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相关推荐

  1. (原創) 如何使用Operator Overloading? (C/C++)

    Abstract Operator Overloading讓我們可以自己定義Operator的功能,讓程式可以更精簡,C#也有,不過不是很強調,但C++非常強調Operator Overloading ...

  2. 面向对象程序设计-C++ Default constructor Copy constructor Destructor Operator Overloading【第九次上课笔记】...

    先上笔记内容吧: 这次上课的内容有关 构造函数 析构函数 运算符重载 return * this 内容很细,大家好好回顾笔记再照应程序复习吧 :) #include <iostream>u ...

  3. C++ operator两种用法【转】

    C++中的operator,有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).下面分别进行介绍: 1.operator ...

  4. operator的两种用法

    C++ operator两种用法 C++中的operator,有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).下面分别 ...

  5. operator 用法

    C++中的operator,有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).下面分别进行介绍: 1.operator ...

  6. C++11中weak_ptr的使用

    在C++中,动态内存的管理是通过一对运算符来完成的:new,在动态内存中为对象分配空间并返回一个指向该对象的指针,可以选择对对象进行初始化:delete,接受一个动态对象的指针,销毁该对象,并释放与之 ...

  7. C++11中unique_ptr的使用

    在C++中,动态内存的管理是通过一对运算符来完成的:new,在动态内存中为对象分配空间并返回一个指向该对象的指针,可以选择对对象进行初始化:delete,接受一个动态对象的指针,销毁该对象,并释放与之 ...

  8. c++重载运算符_Chapter13:重载——入门(一)

    一.走进重载 所谓重载,就是赋予新的含义.函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作.运算符重载(Operator Overloadin ...

  9. c语言面向对象编程中的类_C ++中的面向对象编程

    c语言面向对象编程中的类 Object oriented programming, OOP for short, aims to implement real world entities like ...

最新文章

  1. Nature:人体菌群研究的25个里程碑
  2. 车载360度全景监视系统
  3. 如何用JavaScript实现获取验证码的效果
  4. matplotlib 折线图_漂亮图表也可信手拈来,一文学会用Python绘制堆积折线图
  5. r语言mfrow全程_如何使用R完成文章中图片处理小教程
  6. PyTorch 多机多卡训练:分布式实战与技巧
  7. PowerShell Format-Table的细节(AutoSize和Wrap参数)
  8. TXSQL:云计算时代数据库核弹头——云+未来峰会开发者专场回顾
  9. 46张PPT彻底弄懂JVM、GC算法和性能调优!
  10. hbase 性能优化
  11. matlab练习程序(生成加密p文件)
  12. python编写财务软件_python 与财务
  13. esp8266作为wifi中继器
  14. vue3实现动态组件加载写法
  15. 移动营销必备:App自动绑定的五大场景赋能
  16. DSPE-PEG-TAT,磷脂-聚乙二醇-靶向穿膜肽TAT,一种磷脂PEG肽
  17. 修改ftp服务器地址,ftp服务器ip地址修改
  18. 安卓端录像并将视频分享给微信好友
  19. SwiftUI基础之Text格式化显示小数specifier
  20. 我们的指纹是如何形成的,科学家找到主导指纹形成原因

热门文章

  1. 12 求1+2+...+n
  2. 《设计模式之禅》学习笔记(一)
  3. 简(kun)单(nan)到让我开(jue)心(wang)的后缀自动机全家桶(普通后缀、广义后缀、子序列)...
  4. 08-cmake语法-set()
  5. Python网络编程:IO多路复用
  6. 浅玩JavaScript的数据类型判断
  7. linux下踢出已登录用户
  8. LoaderManager使用详解(三)---实现Loaders
  9. Lazarus IOCP 移植
  10. C# 工厂模式 简单入门