C运算符重载实例Word文档下载推荐.docx
《C运算符重载实例Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C运算符重载实例Word文档下载推荐.docx(11页珍藏版)》请在冰豆网上搜索。
![C运算符重载实例Word文档下载推荐.docx](https://file1.bdocx.com/fileroot1/2022-11/15/6032602c-1d5b-47a5-b618-4a30173aa77c/6032602c-1d5b-47a5-b618-4a30173aa77c1.gif)
stack&
operator=(stack&
rightValue);
};
//栈的容量增倍
voidstack:
:
inflate()
intindex,*tp;
tp=(int*)malloc(sizeof(int)*max*2);
for(index=0;
index<
top;
index++)
tp[index]=sp[index];
max+=max;
free(sp);
sp=tp;
//出栈
intstack:
pop()
if(top<
=0)
throw1;
returnsp[--top];
//入栈
push(intvalue)
if(top==max)
inflate();
sp[top++]=value;
//赋值函数
stack:
operator=(stack&
rightValue)
top=rightValue.top;
max=rightValue.max;
sp=(int*)malloc(sizeof(int)*max);
for(intindex=0;
max;
sp[index]=rightValue.sp[index];
return*this;
voidmain()
stackx(100),y,z;
z=y=x;
这里要注意的是赋值函数的返回值是stack&
,这是为了实现链式表达式,如z=y=x;
。
这一点可见《高质量c/c++编程》(林锐)一书。
2.[]的重载
语法:
值类型operator[](一个形参)函数体
示例程序如下:
iostream>
usingnamespacestd;
classArray
int*a,len;
voidinflate()
cout<
<
"
thearrayisinflating..."
<
endl;
int*b=(int*)malloc(sizeof(int)*len*2);
for(inti=0;
i<
len;
i++)
{
b[i]=a[i];
}
len+=len;
free(a);
a=b;
Array(intsize):
len(size)
a=(int*)malloc(sizeof(int)*size);
int&
operator[](intindex)
if(index<
0)
throw1;
if(index>
=len)
inflate();
returna[index];
voidtrans()
a[i]<
Arraya(15);
for(inti=0;
20;
i++)
a[i]=i;
a.trans();
3.()的重载
示例程序代码:
stdlib.h>
time.h>
//求最小值
template<
typenameT1,typenameT2>
T1&
min(T1*x,intn,T2cmp)
intj=0;
for(inti=1;
n;
if(cmp(x[i],x[j]))
j=i;
returnx[j];
//函数对象
classFun_obj
booloperator()(intx,inty)
returnx<
y;
intcmp(intx,inty)
returnx<
inta[]={0,1,7,9,5,4,-2,3};
intlen=sizeof(a)/sizeof(int);
Fun_objfo;
//cout<
minbyfunction_object:
"
//min(a,sizeof(a)/sizeof(int),fo)<
minbyfunction_pointer:
//min(a,sizeof(a)/sizeof(int),cmp)<
longnum=100000000;
clock_tstart,finish;
doubleduration;
start=clock();
num;
min(a,len,fo);
finish=clock();
duration=(double)(finish-start);
///CLOCKS_PER_SEC;
cout<
minbyfunction_object:
duration<
seconds"
min(a,len,cmp);
minbyfunction_pointer:
函数对象只是重载了成员函数运算符(),而类成员函数均为inline函数,inline函数在编译时展开;
使用函数指针指向的函数是有一个函数调用和函数返回的过程的,所以使用函数对象比使用一般的函数效率要高。
4.->
的重载
值类型operator->
()函数体
返回值必须是
(1)指针或
(2)类类型的对象或者对象的引用
调用语法:
object->
member
执行过程:
(1)如果object是指针,所指对象需要有member成员;
(2)object是一个对象或对象引用,所属类必须重载->
计算->
,得到obj(指针),做object=obj,转
(1)。
例如:
classA
intx;
A(intvalue):
x(value)
A*operator->
()
returnthis;
classB
A&
a;
B(A&
value):
a(value)
operator->
returna;
Aa(99);
Bb(a);
b->
x<
示例程序
(二):
classNode
intinfo;
Node*next;
Node(intvalue):
info(value)
classiter
Node*p,*q;
iter(Node*u)
p=u;
q=NULL;
iter()
p=q=NULL;
iter&
operator++()
if(p!
=NULL)
q=p;
p=p->
next;
return*this;
operator++(int)
returnoperator++();
booloperator!
=(iteri)
returnp!
=i.p;
Node*operator->
returnp;
iterbegin()
returniter(this);
iterend()
returniter();
voidprint()
iterp=begin();
iterq=end();
while(p!
=q)
p->
info<
p++;
voidinsert(Node**h)
iteri=(*h)->
begin(),j=(*h)->
end();
while(i!
=j&
&
i->
this->
info)
i++;
next=i.p;
if(i.q==NULL)
*h=this;
else
i.q->
next=this;
Node*head=NULL;
(newNode
(2))->
insert(&
head);
(newNode(3))->
(newNode
(1))->
(newNode(4))->
head->
print();
这里需要注意的是,在C++程序中,重载“++”和“--”运算符正确理解它们的语义很重要。
intb=++a;
语义:
a+=1;
intb=a;
而
intb=a++;
语义是:
inttemp=a;
intb=temp;
temp.~int();
而不是