设为首页 加入收藏

TOP

操作符重载(二)(一)
2019-09-24 11:16:09 】 浏览:104
Tags:操作 重载

1. 数组操作符重载

数组操作符重载

通过重载数组操作符,可以使类的对象支持数组的下标访问

  • 数组操作符只能重载为类的成员函数
  • 重载函数能且仅能使用一个参数,也就是数组下标
  • 可以定义不同参数的多个重载函数

在重载数组操作符时,要记得数组操作符的原生语义——数组访问指针运算
a[n] <--> *(a + n) <--> *(n + a) <--> n[a]

#include <iostream>
#include <string>

using namespace std;

class Test
{
    int a[3];
public:
    int &operator [] (int i)
    {
        return a[i];
    }

    int &operator [] (const string &s)
    {
        if( s == "1st" )
        {
            return a[0];
        }
        else if( s == "2nd" )
        {
            return a[1];
        }
        else if( s == "3rd" )
        {
            return a[2];
        }

        return a[0];
    }
};

int main()
{
    Test t;

    for (int i = 0; i < 3; i++)
    {
        t[i] = i;
    }

    for (int i = 0; i < 3; i++)
    {
        cout << t[i] << endl;
    }

    cout << endl;

    cout << t["3rd"] << endl;
    cout << t["2nd"] << endl;
    cout << t["1st"] << endl;

    return 0;
}

数组类IntArray改进

IntArray.h

class IntArray
{
public:
    int &operator [] (int index); //Add
    IntArray &self();             //Add
};

IntArray.cpp

int &IntArray::operator [] (int index)
{
    return m_pointer[index];
}

IntArray &IntArray::self()
{
    return *this;
}

2. 函数操作符重载(函数对象)

  • 函数操作符只能通过类的成员函数重载
  • 可以定义不同参数的多个重载函数
  • 函数操作符重载的本质是使用具体的类对象取代函数,也就是函数对象,函数对象具备函数调用的行为
  • 函数对象用于在工程中取代函数指针
/*
 * 本示例代码实现以下需求:
 * - 编写一个函数,可以获得斐波那契数列每项的值
 * - 每调用一次返回一个值
 * - 函数可根据需要重复使用
*/

#include <iostream>
#include <string>

using namespace std;

class Fib
{
    int a0;
    int a1;
public:
    Fib()
    {
        a0 = 0;
        a1 = 1;
    }

    Fib(int n)
    {
        a0 = 0;
        a1 = 1;

        for(int i = 2; i <= n; i++)
        {
            int t = a1;

            a1 = a0 + a1;
            a0 = t;
        }
    }

    int operator () ()
    {
        int ret = a1;

        a1 = a0 + a1;
        a0 = ret;

        return ret;
    }
};

int main()
{
    Fib fib;

    for(int i = 0; i < 10; i++)
    {
        cout << fib() << endl;
    }

    cout << endl;

    for(int i = 0; i < 5; i++)
    {
        cout << fib() << endl;
    }

    cout << endl;

    Fib fib2(10);

    for(int i = 0; i < 5; i++)
    {
        cout << fib2() << endl;
    }

    return 0;
}

3. 指针操作符重载与智能指针

指针操作符重载

指针操作符指的是->*

  • 指针操作符只能通过类的成员函数重载
  • 重载函数不能使用参数,也就是说,只能定义一个重载函数

智能指针

  • 利用指针操作符重载,可以实现智能指针
  • 智能指针只能用来指向堆空间中的对象或者变量!!!
  • 智能指针在生命周期结束时会自动释放堆空间
  • 智能指针的意义在于减少开发人员的内存管理工作,最大程度上避免内存问题。
/*
 * 实现智能指针类Pointer,要求如下:
 *  - 一片堆空间最多只能由一个指针标识
 *  - 禁止指针运算和指针比较
*/

#include <iostream>
#include <string>

using namespace std;

class Test
{
    int i;
public:
    Test(int i)
    {
        cout << "Test(int i)" << endl;
        this->i = i;
    }
    int value()
    {
        return i;
    }
    ~Test()
    {
        cout << "~Test()" << endl;
    }
};

class Pointer
{
private:
    Test *mp;
public:
    Pointer(Test *p = NULL)
    {
        mp = p;
    }

    Pointer(const Pointer &obj)
    {
        mp = obj.mp;
        const_cast<Pointer &>(obj).mp = NULL;
    }

    Pointer &operator = (const Pointer &obj)
    {
        if (this != &obj)
        {
            delete mp;
            mp = obj.mp;
            const_cast<Pointer &>(obj).mp = NULL;
        }

        return *this;
    }

    Test *operator -> ()
    {
        return mp;
    }

    Test &operator * ()
    {
        return *mp;
    }

    bool isNull()
    {
        return (mp == NULL);
    }

    ~Pointer()
    {
        delete mp;
    }
};

int main()
{
    Pointer p1 = new Test(0);
    Pointer p2 = p1;

    cout << p1.isNull() << endl;
    cout << p2->value() << endl;

    Pointer p3;
    p3 = p2;

    cout <&l
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++—多态与继承 下一篇C语言单链表简单实现(简单程序复..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目