对于简单的运算符,可以参考之前的博文。之后会有一篇关于从等号运算符重载的角度研究深浅拷贝的博文。
这里是讲:逗号,取成员运算符,输入输出运算符,下标运算符,括号,new和delete的重载。

逗号运算符重载

逗号运算符重载需要一个参数,并且返回自身类。逗号运算符在复制操作中比较常见,下面就是以赋值操作为例的逗号运算符重载。

#include<string>
#include<iostream>
using namespace std;
class Tem{
private:
int x;
public:
Tem(int);
Tem operator ,(Tem);
void display();
}; Tem::Tem(int xx=0){
x = xx;
} Tem Tem::operator , (Tem t){
cout<<t.x<<endl;
return Tem(t.x);
}
void Tem::display(){
cout<<"Class(Tem) includes of: "<<x<<endl;
} int main(){ Tem tem1(10);
Tem tem2(20);
Tem other = (tem1,tem2); //会选择第二个 int a= 1,2;a等于2而不是1
other.display(); return 0;
}

取成员运算符重载

返回类类型的指针变量,符合平时的用法,这样就可以不用在声明变量时候使用指针,但是之后可以按照指针的方式调用,简单方便。

#include<iostream>
using namespace std; class Tem{
public:
int n;
float m;
Tem* operator ->(){
return this;
}
};
int main(){
Tem tem;
tem->m = 10; //调用->运算符,返回Tem*类型并访问m
cout<<"tem.m is "<<tem.m<<endl;
cout<<"tem->m is "<<tem->m<<endl;
return 0;
}

输入输出运算符重载

>>,<<运算符重载分别在cin、cout之后调用。我们需要用友元运算符对他们进行重载,注意返回类型分别是istream 和 ostream。

#include<iostream>
using namespace std; class Date{
private:
int year,month,day;
public:
Date (int y,int m,int d){
year = y;
month = m;
day = d;
}
friend ostream& operator <<(ostream &stream,const Date &date){
stream<<date.year<<" "<<date.month<<" "<<date.day<<endl;
return stream;
}
friend istream& operator >>(istream &stream,Date &date){
stream>>date.year>>date.month>>date.day;
return stream;
}
};
int main(){
Date _date(2017,6,13);
cout<<_date;
cin>>_date;
cout<<_date;
return 0;
}

下标运算符重载

下标运算符只能被重载为类的非静态成员函数,不能重载为友元函数和普通函数。

下标运算符只能有一个参数,多和数组有关,返回引用类型可以查看和修改数组元素。

#include<iostream>
#include<string>
using namespace std; const int LEN = 3; class Tem{
private:
int a[LEN];
public:
Tem(){
for (int i=0;i<LEN;++i)
a[i] = i;
}
int &operator [](int i){
return a[i];
}
}; int main(){
Tem tem;
cout<<tem[1]<<endl;
tem[1] = -1;//修改
cout<<tem[1]<<endl;//重新查看
return 0;
}

括号运算符重载

括号运算符只能被重载为类的非静态成员函数。并且参数个数和返回类型没有限制

所以我们可以实现很多脚本语言中或者string库里面的切片操作。

#include<iostream>
#include<string>
using namespace std; const int LEN = 10; class Tem{
private:
int a[LEN];
public:
Tem(){
for (int i=0;i<LEN;++i)
a[i] = i;
}
int &operator [](int i){
return a[i];
}
int * operator ()(int start ,int end){ //用括号运算符实现切片操作
int *t = new int [end-start];
for(int i=start;i<end;++i)
t[i-start] = a[i];
return t;
}
}; int main(){
Tem tem; int *arr = tem(0,5);
for(int i=0;i<5;++i){
cout<<arr[i]<<" ";
}
cout<<endl; return 0;
}

new和delete运算符重载

注意new和delete重载的时候参数的形式和返回类型即可。

#include<iostream>
#include<cstddef>
using namespace std; const int LEN = 10;
class Tem{
private:
int a[LEN];
public:
Tem(){
for (int i=0;i<LEN;++i)
a[i] = i;
}
void *operator new(size_t size); //开辟大小为size的空间
void *operator new(size_t size,char p); //开辟大小为size的空间,并且每个都赋值为p
void operator delete(void *p);
};
void * Tem::operator new(size_t size){//参数是无符号类型,在cstddef里,是c++里的unsigned
cout<<"Create a place includes of "<<size<<" bytes\n";
char *s = new char[size];
*s = 'a';
return s;
} void * Tem::operator new(size_t size,char p){ //返回类型是可以自动转化的指针类型
cout<<"Create a place includes of "<<size<<" bytes\n";
char *s = new char[size];
for(int i=0;i<size;i++)
s[i] = p;
return s;
} void Tem::operator delete(void *p){
cout<<"Delete \n";
delete [] p; //最后释放空间
}
int main(){
Tem *tem = new Tem;
delete tem; Tem *_tem = new('E') Tem;
delete _tem; return 0;
}

欢迎进一步交流本博文相关内容:

博客园地址 : http://www.cnblogs.com/AsuraDong/

CSDN地址 : http://blog.csdn.net/asuradong

也可以致信进行交流 : xiaochiyijiu@163.com

欢迎转载 , 但请指明出处  :  )


深入C++的运算符重载的更多相关文章

  1. C++ 运算符重载时,将运算符两边对象交换问题.

    在C++进行运算符重载时, 一般来讲,运算符两边的对象的顺序是不能交换的. 比如下面的例子: #include <iostream> using namespace std; class ...

  2. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

  3. C++运算符重载

    C++运算符重载 基本知识 重载的运算符是具有特殊名字的函数,他们的名字由关键字operator和其后要定义的运算符号共同组成. 运算符可以重载为成员函数和非成员函数.当一个重载的运算符是成员函数时, ...

  4. 标准C++之运算符重载和虚表指针

    1 -> *运算符重载 //autoptr.cpp     #include<iostream> #include<string> using namespace std ...

  5. python运算符重载

    python运算符重载就是在解释器使用对象内置操作前,拦截该操作,使用自己写的重载方法. 重载方法:__init__为构造函数,__sub__为减法表达式 class Number: def __in ...

  6. PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天

    PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数    1  构造  2  析构   3  赋值  4 拷贝构造  5 oper ...

  7. 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换

    [源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...

  8. 我的c++学习(8)运算符重载和友元

    运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...

  9. c/c++面试题(6)运算符重载详解

    1.操作符函数: 在特定条件下,编译器有能力把一个由操作数和操作符共同组成的表达式,解释为对 一个全局或成员函数的调用,该全局或成员函数被称为操作符函数.该全局或成员函数 被称为操作符函数.通过定义操 ...

  10. 实验12:Problem H: 整型数组运算符重载

    Home Web Board ProblemSet Standing Status Statistics   Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...

随机推荐

  1. web 前端学习笔记

    <!DOCTYPE HTML> <head> <style type="text/css"> body {  background: #ff00 ...

  2. WCF学习笔记——配置服务引用

    WCF传过来的东西要序列化. 比如,在WCF服务中,象这么个方法 public IEnumerable<UserItem> GetUserList() 默认情况下,在客户端会调用,是这样: ...

  3. C# 手动编写 DataSet,DataTable 及遍历DataSet中的数据

    一.手动编写DataSet:    有时候不想从数据库导出 DataSet,或者有其他的需要,要将数据库里的DataSet包装成另一个样子,这个时候,了解DataSet的内部结构就非常必要.DataS ...

  4. DCloud-MUI:AJAX

    ylbtech-DCloud-MUI:AJAX 1.返回顶部 1.   2. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. http://dev.dcloud.net.cn ...

  5. B. Sereja and Suffixes(cf)

    http://codeforces.com/problemset/problem/368/B B. Sereja and Suffixes time limit per test 1 second m ...

  6. POJ 2823 线段树 Or 单调队列

    时限12s! 所以我用了线段树的黑暗做法,其实正解是用单调队列来做的. //By SiriusRen #include <cstdio> #include <cstring> ...

  7. A - Voting(queue)

    Problem description There are n employees in Alternative Cake Manufacturing (ACM). They are now voti ...

  8. angularJS之ng-bind与{{}}取值的区别

    1:{{ }} 是等页面加载完后,再取值. 2:ng-bind 它是在页面加载的时候,是不会显示{{name}}这种变量出来. 3:ng-bind 可以解决 ng 页面闪烁加载问题. 4:ng-bin ...

  9. (转) 前端模块化:CommonJS,AMD,CMD,ES6

    模块化的开发方式可以提高代码复用率,方便进行代码的管理.通常一个文件就是一个模块,有自己的作用域,只向外暴露特定的变量和函数.目前流行的js模块化规范有CommonJS.AMD.CMD以及ES6的模块 ...

  10. ThreadPoolExecutor理解

    ThreadPoolExecutor组成 ThreadPoolExecutor的核心构造函数: public ThreadPoolExecutor(int corePoolSize, int maxi ...