操作符的重载(下)

来源:互联网 发布:中石化定额软件 编辑:程序博客网 时间:2024/06/05 19:51

opreator+的成员函数实现:

#include <cstdlib>
#include <iostream>
//操作符重载分成员函数重载和全局函数重载
//当无法更改函数左操作数的类时,使用全局函数重载

using namespace std;
class Complex
{
    int a;
    int b;
public:
    Complex(int a, int b)
    {
        this->a = a;
        this->b = b;
    }
    int getA()
    {
        return a;
    }
    int getB()
    {
        return b;
    }
    Complex operator+ (const Complex& c2);//成员函数重载
    friend ostream& operator<< (ostream& out, const Complex& c);//ostream是c++库中的类,不可更改,因此左移运算符只能使用全局函数重载
};

ostream& operator<< (ostream& out, const Complex& c)

{
    out<<c.a<<" + "<<c.b<<"i";  
    return out;
}
Complex Complex::operator+ (const Complex& c2)//使用成员函数重载,this指针替代左操作数
{
    Complex ret(0, 0);   
    ret.a = this->a + c2.a;
    ret.b = this->b + c2.b;//类的成员函数默认提供this指针
    return ret;
}
int main(int argc, char *argv[])
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c1 + c2;   
    cout<<c1<<endl;
    cout<<c2<<endl;
    cout<<c3<<endl;
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

用成员函数重载的操作符:

比全局函数重载的操作符少一个参数,也就是左操作数;

不需要使用friend关键字;


操作符重载:

c++中操作符重载均通过函数实现,问题在于什么时候使用全局函数,什么时候使用成员函数;

当无法修改左操作数的类时,使用全局函数进行重载;

= 【】 ()和 ->操作符只能通过成员函数进行重载;


数组类的改进:

#ifndef _ARRAY_H_
#define _ARRAY_H_
class Array
{
private:
    int mLength;
    int* mSpace;
public:
    Array(int length);
    Array(const Array& obj);
    int length();
    ~Array();
    int& operator[](int i);//数组操作运算符重载,使用引用可以让函数调用作为左值使用
    Array& operator= (const Array& obj);//赋值运算符重载,使用引用可以使当前返回值的对象连续赋值,否则编译错误
    bool operator== (const Array& obj);
    bool operator!= (const Array& obj);//比较运算符重载,返回值为1或0,采用bool类型
};
#endif


#include <stdio.h>
#include "Array.h"
Array::Array(int length)
{
    if( length < 0 )
    {
        length = 0;
    }
    mLength = length;
    mSpace = new int[mLength];
}
Array::Array(const Array& obj)
{
    mLength = obj.mLength;   
    mSpace = new int[mLength];
    for(int i=0; i<mLength; i++)
    {
        mSpace[i] = obj.mSpace[i];
    }
}
int Array::length()
{
    return mLength;
}
Array::~Array()//析构
{
    mLength = -1;
    printf("%08X\n", mSpace);
    delete[] mSpace;
}
int& Array::operator[](int i)
{
    return mSpace[i];
}
Array& Array::operator= (const Array& obj)
{
    delete[] mSpace;//先释放自己空间   
    mLength = obj.mLength;
    mSpace = new int[mLength];//再为obj对象申请堆空间
    for(int i=0; i<mLength; i++)
    {
        mSpace[i] = obj.mSpace[i];
    }
    return *this;//返回自己,自己就是this指针;
}
bool Array::operator== (const Array& obj)
{
    bool ret = true;   
    if( mLength == obj.mLength )//比较长度
    {
        for(int i=0; i<mLength; i++)//内容比较
        {
            if( mSpace[i] != obj.mSpace[i] )
            {
                ret = false;
                break;
            }
        }
    }
    else
    {
        ret = false;
    }
    return ret;
}
bool Array::operator!= (const Array& obj)
{
    return !(*this == obj);//利用==运算符重载
}

#include <stdio.h>
#include "Array.h“
int main()
{
    Array a1(10);
    Array a2(0);
    Array a3(0);
    if( a1 != a2 )
    {
        printf("a1 != a2\n");
    }
    for(int i=0; i<a1.length(); i++)
    {
        a1[i] = i + 1;//a1.opreator[](i),此处需要调用函数作为左值使用,因此调用函数返回值必须为引用
    }
    for(int i=0; i<a1.length(); i++)
    {
        printf("Element %d: %d\n", i, a1[i]);
    }
    a3 = a2 = a1;//连续赋值,等价于a3 = (a2.opreator=(a1));
    if( a1 == a2 )
    {
        printf("a1 == a2\n");
    }
    for(int i=0; i<a2.length(); i++)
    {
        printf("Element %d: %d\n", i, a2[i]);
    }  
    printf("Press any key to continue...");
    getchar();
    return 0;
}

c++编译器会为每个类提供默认的赋值操作符;

默认的赋值操作符只是做简单的值复制;

类中存在指针成员变量时就需要重载赋值操作符;

++操作符的重载:

++操作符只有一个操作符,并且有前置和后置之分;

c++中通过一个占位参数来区分前置和后置;

#include <cstdlib>
#include <iostream>
using namespace std
class Complex
{
    int a;
    int b;
public:
    Complex(int a, int b)
    {
        this->a = a;
        this->b = b;
    }
    int getA()
    {
        return a;
    }
    int getB()
    {
        return b;
    }
    Complex operator+ (const Complex& c2);
    Complex operator++ (int);
    Complex& operator++();  
    friend ostream& operator<< (ostream& out, const Complex& c);
};
ostream& operator<< (ostream& out, const Complex& c)
{
    out<<c.a<<" + "<<c.b<<"i";   
    return out;
}
Complex Complex::operator++ (int)//后置++操作符实现方式
{
    Complex ret = *this;//利用拷贝构造函数将当前对象拷贝到ret中
    a++;//this->a++;
    b++;//this->b++;
    return ret;
    //上述过程类似于c语言,int c = 0;c++;
}

Complex& Complex::operator++()//前置++
{
    ++a;
    ++b;   
    return *this;///此过程类似于c语言中前置++定义,前置++效率更高,后置++效率低的多
}
Complex Complex::operator+ (const Complex& c2)
{
    Complex ret(0, 0);   
    ret.a = this->a + c2.a;
    ret.b = this->b + c2.b;
    return ret;
}
int main(int argc, char *argv[])
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c2;   
    c2++;
    ++c3;
    cout<<c1<<endl;
    cout<<c2<<endl;
    cout<<c3<<endl;
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

c++中的&&和||操作符:

&&和||内置了短路规则;

操作符重载是靠函数重载实现的;

操作数作为函数参数传递;

c++的函数参数都会被求值,无法实现短路规则;


最好不要重载&&和||操作符,因为虽然语法上没有错误,但是&&,||对于一个开发人员来说都是烂熟于心的,这样重载之后只会带来很多不必要的麻烦,降低程序的可读性,并还很可能产生一堆bug!!!

操作符重载可以直接使用类的成员函数实现
=, [], ()和->操作符只能通过成员函数进行重载
++操作符通过一个int参数进行前置与后置的重载
C++中不要重载&&和||操作符

0 0
原创粉丝点击