重载(二),运算符重载

重载前缀运算符

returnType operator op(parameter)

重载后缀运算符

后缀运算符加一个整变量作为参数,程序忽略参数的值。

重载运算符的例子:

#include <iostream>
using namespace std;

typedef unsigned 
short USHORT;

class Counter
{
public:
    Counter();
    Counter(USHORT val);
    
~Counter() {}
    USHORT GetItsVal() 
const return itsVal; }
    
void SetItsVal(USHORT x) { itsVal = x; }
    
void Increment() ++itsVal; }
    Counter Add(
const Counter&);

    
const Counter& operator ++(); // 重载前缀运算符
    const Counter& operator ++(int); // 重载后缀运算符
    Counter operator +(const Counter&);

private:
    USHORT itsVal;
}
;

Counter::Counter():
    itsVal(
0{ };

Counter::Counter(USHORT val):
    itsVal(val) 
{ };

Counter Counter::Add(
const Counter& rhs) {
    
return Counter(itsVal + rhs.GetItsVal());
}


const Counter& Counter::operator ++() {
    
++itsVal;
    
return *this;
}


const Counter& Counter::operator ++(int x) {
    itsVal
++;
    
return *this;
}


Counter Counter::
operator +(const Counter& rhs) {
    
return Counter(itsVal + rhs.GetItsVal());
}


int main(int argc, char *argv[])
{
    Counter i;
    cout 
<< i.GetItsVal() << endl;
    i.Increment();
    cout 
<< i.GetItsVal() << endl;
    
++i;
    cout 
<< i.GetItsVal() << endl;
    i
++;
    cout 
<< i.GetItsVal() << endl;
   
    Counter j(
3);
    
//Counter k = i.Add(j);
    Counter k = i + j;
    cout 
<< "k: " << k.GetItsVal() << endl;

    
return 0;
}


运算符重载中的问题

重载运算符既可以是函数成员,也可以是非函数成员。必须是类成员的运算符有:赋值(=),下标([ ]),函数调用(()) 和间接(-〉)运算符。

对运算符重载的限制

内置类型的运算符不可以重载。
运算符优先级不能被改变。
是单目还是双目的不能被改变。
不能创建新运算符。

重载赋值运算符 =

#include <iostream>
using namespace std;

class CAT
{
public:
    CAT();
    
// 省略了复制构造函数和析购函数
    
    
int GetAge() const return *itsAge; }
    
int GetWeight() const return *itsWeight; }
    
void SetAge(int age) *itsAge = age; }
    CAT 
operator = (const CAT&);

private:
    
int* itsAge;
    
int* itsWeight;
}
;

CAT::CAT() 
{
    itsAge 
= new int;
    itsWeight 
= new int;
    
*itsAge = 5;
    
*itsWeight = 9;
}


CAT CAT::
operator = (const CAT& rhs) {
    
// 这个判断非常重要! 防止产生自己向自己赋值的情况.比如 a = a;
    if (this == &rhs)
    
{
        
return *this;
    }

    itsAge 
= new int;
    itsWeight 
= new int;
    
*itsAge = rhs.GetAge();
    
*itsWeight = rhs.GetWeight();
}




int main(int argc, char *argv[])
{
    CAT frisky;
    cout 
<< "frisky's age: " << frisky.GetAge() << endl;
    cout 
<< "Setting frisky to 6\n";
    frisky.SetAge(
6);
    CAT whiskers;
    cout 
<< "whiskers' age: " << whiskers.GetAge() << endl;
    cout 
<< "copying frisky to whiskers\n";
    whiskers 
= frisky;
    cout 
<< "whiskers' age: " << whiskers.GetAge() << endl;
     
    
return 0;
}


转换运算符

类型转换:

USHORT -> Counter

#include <iostream>
using namespace std;

typedef unsigned 
short USHORT;

class Counter
{
public:
    Counter();
    
// 添加一个构造函数来用于隐式类型转换
    Counter(USHORT);
    
~Counter() { };
    USHORT GetItsVal() 
const return itsVal; }
    
void SetItsVal(USHORT x) { itsVal = x; }

private:
    USHORT itsVal;
}
;

Counter::Counter():
    itsVal(
0{ }

Counter::Counter(USHORT val):
    itsVal(val) 
{ }

int main(int argc, char *argv[])
{
    USHORT theShort 
= 5;
    Counter theCtr 
= theShort;
    cout 
<< "theCtr: " << theCtr.GetItsVal() << endl;
     
    
return 0;
}


Counter -> USHORT 的转换:

#include <iostream>
using namespace std;

typedef unsigned 
short USHORT;

class Counter
{
public:
    Counter();
    
// 添加一个构造函数来用于隐式类型转换
    Counter(USHORT val);
    
~Counter() { };
    USHORT GetItsVal() 
const return itsVal; }
    
void SetItsVal(USHORT x) { itsVal = x; }
    
    
// 用于隐式类型转换。注意不指定返回值类型。
    operator unsigned short();

private:
    USHORT itsVal;
}
;

Counter::Counter():
    itsVal(
0{ }

Counter::Counter(USHORT val):
    itsVal(val) 
{ }

Counter::
operator unsigned short() 
    
return USHORT(itsVal);
}


int main(int argc, char *argv[])
{
    Counter ctr(
5);
    USHORT theShort 
= ctr;
    cout 
<< "theShort: " << theShort << endl;
     
    
return 0;
}



posted on 2005-04-26 14:36  NeilChen  阅读(804)  评论(0编辑  收藏  举报

导航