Chinaunix首页 | 论坛 | 博客
  • 博客访问: 242455
  • 博文数量: 91
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-12 09:38
文章分类

全部博文(91)

文章存档

2017年(1)

2011年(1)

2008年(15)

2007年(74)

我的朋友

分类: LINUX

2008-03-01 16:33:01

运算符的重载
可以将运算符重载为成员函数或友元函数,一元运算符应重载为成员,有些二元,如:“=”,"()""[]""->"等只能重载为成员函数;而如“>=,"*="等含有等号的二元通常重载为成员函数
其它一般重载为友元函数
一元运算符的重载:
以下的两个程序注意比较,看看他们的结果,第一个通过直接输出,而第二个通过返回后,再通过全局函数调用
#include <iostream.h>
class integer
{
private:
int i;
int a;
public:
integer(int x,int f)
{
i=x;
a=f;
}
void display()const //this is their different
{
cout<<"i= "<<i<<endl;
cout<<"a= "<<a<<endl;
}
integer& operator++() //overload operator ++i
{
i++;
a++;
return *this;
}
integer operator++(int) //overload operator i++
{
int odi=i;
int odi1=a;
i++;
a++;
return integer(odi,odi1);
}
};
int main()
{
integer in(12,5);
in++;
in.display();
++in;
in.display();
return 0;
}
********the result:***********
i= 13
a= 6
i= 14
a= 7
从以上结果 可以看出,显然不是我们想要的结果,以下是修改后的结果
*********************
#include <iostream.h>
class integer
{
private:
int i;
int a;
public:
integer(int x,int f)
{
i=x;
a=f;
}
integer()
{}
int geti() //append return
{
return i;
}
int geta() //append return
{return a;
}
void display()const //it not invoking
{
cout<<"i= "<<i<<endl;
cout<<"a= "<<a<<endl;
}
integer& operator++() //overload operator ++i
{
i++;
a++;
return *this;
}
integer operator++(int) //overload operator i++
{
int odi=i;
int od=a;
i++;
a++;
return integer(odi,od);
}
};
void output(integer ux) //define a grobal function and the object can't modified quote
{            //example: void output(integer &ur), it will running error
cout< }
int main()
{
integer in(12,25);
//in++;
output(in++);
output(++in);
output(in++);
return 0;
}
*******the result*******
12 25
14 27
14 27
从以上两个程序可以看出,再用重载操作符(一元)时,最好定义一个返回函数,然后再通过外部调用,否则会出错
类似的操作符还有operator--(), operator--(int).
在以上类中添加如下语句
integer& operator--() //overload operator i--
{
i--;
a--;
return *this;
}
integer operator--(int) //overload operator --i
{
int odi=i;
int od=a;
i--;
a--;
return integer(odi,od);
}
integer& operator-() //取相反数
{
i=i*(-1);
a=a*(-1);
return *this;
}
integer& operator~() //按位取非
{
i=~i;
a=~a;
return *this;
}
integer& operator!() //取反
{
i=!i;
a=!a;
return *this;
}

在以上main函数中添加如下的语句:
output(in--);
output(--in);
output(in--);
output(-in);
output(~in);
output(!in);
*********the result:*************
12 25
10 23
10 23
-9 -22
8 21
0 0
以上结果不包括自加的结果

二元运算符(+=, + 和-=)
类中添加如下语句(还是利用上面的程序)
integer& operator+=(integer &c) //overload operator i++
{
i+=c.geti();
a+=c.geta();
return *this;
}
integer &operator-=(integer &x1) //overload operator ++i
{
i-=x1.geti(); //在这里面其实是两个对象,一个是this所指的对象,一个是参数中的对象。
a-=x1.geta();
return *this;
}
integer operator+(integer c) //overload operator i++, need a parameter, don'
t two object parameters
{                //because have a this pointer object
int a1=c.geti()+i;
int a2=c.geta()+a;
return integer(a1,a2);
}

在main函数中的调用方法
integer tes1(12,25);
integer tes2(11,11);
integer tes3;
注意,在以下重载加法中,如果是成员函数,就只需一个参数,如果是友元,则需要两个参数。
tes3=tes1+tes2; //invoking the operator+();
tes2+=tes1; //invoking the operator+=();
tes2-=tes1; //invoking the operator-=();
output(tes2);
output(tes1);
重载操作符(*=, /=, -, *, /)和以上的调用方法和定义相同。
假如将上面的重载操作符+改为友元的定义方法如下:
在类中添加一个声明
friend integer operator+(integer&, integer&);
在类外定义友元
integer operator+(integer &c, integer &c1)
{
int a1=c.geti()+c1.geti();
int a2=c.geta()+c1.geta();
return integer(a1,a2);
}
在main 函数中的调用相同(与用成员函数定义)
一般情况下,当运算符会影响到自身时,将运算符重载为成员函数,而当运算的结果是一个新的对象时,通常应当把运算符重载为友元函数。
当你想在类中定义一个常量,而又想在类中就将它初始化时,你会怎么做呢?
when you define a const variable in a class, but you don't want to initialize it outer, what will you do?
最佳方法就是定义一个枚举类型
the best method is define a enum
********illustrate********
#include
class test
{
enum{pi=35};
public:
void display()
{
cout< }
};
int main()
{
test tes;
tes.display();
return 0;
}
assert()的调用方法,需要头文件
 ASSERT()是一个调试程序时经常使用的宏,在程序运行时它计算括号内的表达式,如果表达式为FALSE (0), 程序将报告错误,并终止执行。如果表达式不为0,则继续执行后面的语句。这个宏通常原来判断程序中是否出现了明显非法的数据,如果出现了终止程序以免导致严重后果,同时也便于查找错误。例如,变量n在程序中不应该为0,如果为0可能导致错误,你可以这样写程序:
    ......
    ASSERT( n != 0);
    k = 10/ n;
申请空间时可以用变量,如:
#include
#include
int main()
{
int *p;
int ax;
p=new int[ax];
assert(p);
delete[] p;
return 0;
}
********operator=************
在以上类中添加如下语句
private:
int ;
public:
void operator =(const test &p)
{
if(this!=&p)//防止复制自身
x=p.x;
}
*******operator()*********
operator()的参数个数没有限制,可以是任意多个
 #include
                                                                                
    class F
    {
    int i,k;
    public:
   F(int a,int f)
    {
    i=a;
    k=f;
    }
    double operator ()(double x, double y) const; //define a operator()
    };
                                                                                
    double F::operator ()(double x, double y) const
    {
    return (x+5)*y;
    }
                                                                                
    int main()
    {
    F f(3,5); //don'
t affect the operator()'s result

cout<     return 0;
     }
 由于C语言的数组中并没有保存其大小,因此,不能对数组元素进行存取范围的检查,无法保证给数组动态赋值不会越界。利用C++的类可以定义一种更安全、功能强的数组类型。为此,为该类定义重载运算符[]。

    下面先看看一个例子:

    #include

    class CharArray
    {
    private:
    int Length;
    char * Buff;
    public:
    CharArray(int l)
    {
        Length = l;
        Buff = new char[Length];
    }
    ~CharArray() { delete Buff; }
    int GetLength() { return Length; }
    char & operator [](int i);
   
    };

    char & CharArray::operator [](int i)
    {
    static char ch = 0;
    if(i=0)
        return Buff[i];
    else
    {
        cout<<"\nIndex out of range.";
        return ch;
    }
    }

    void main()
    {
    int cnt;
    CharArray string1(6);
    char * string2 = "string";
    for(cnt=0; cnt<8; cnt++)
        string1[cnt] = string2[cnt];  //invoking the overload operator[]() and assignment
    cout<<"\n";
    for(cnt=0; cnt<8; cnt++)
        cout<    cout<<"\n";
    cout<    }

    该数组类的优点如下:

    (1) 其大小不是一个常量。
    (2) 运行时动态指定大小,可以不用运算符new和delete。
    (3) 当使用该类数组作函数参数时,不必分别传递数组变量本身及其大小,因为该对象中已经保存大小。

    在重载下标运算符函数时应该注意:

    (1) 该函数只能带一个参数,不可带多个参数。
    (2) 不得重载为友元函数,必须是非static类的成员函数。

阅读(2191) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~