【C++基础入门】29.C++中数组操作符的重载

文章目录

一、字符串类的兼容性

二、重载数组访问操作符

三、小结


一、字符串类的兼容性

        问题:string 类对象还具备 C 方式字符串的灵活性吗?还能直接访问单个字符吗

  • string最大限度的考虑了 C 字符串的兼容性
  • 可以按照使用 C 字符串的方式使用 string 对象

         下面看一个用 C 方式使用 string 类的示例:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s = "a1b2c3d4e";
    int n = 0;
    
    for (int i = 0; i < s.length(); i++)
    {
        if (isdigit(s[i]))
        {
            n++;
        }
    }
    
    cout << n << endl;
    
    return 0;
}

         输出结果如下:

 

二、重载数组访问操作符

问题:类的对象怎么支持数组的下标访问?

  • 被忽略的事实
    • 数组访问符是 C/C++ 中的内置操作符
    • 数组访问符的原生意义是数组访问指针运算

         下面进行指针与数组的复习:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a[5] = {0};
    
    for (int i = 0; i < 5; i++)
    {
        a[i] = i;
    }
    
    for (int i = 0; i < 5; i++)
    {
        cout << *(a + i) << endl;  //cout << a[i] << endl;
    }
    
    cout << endl;
    
    for (int i = 0; i < 5; i++)
    {
        i[a] = i + 10;  //cout << a[i] <<endl;
    }
    
    for (int i = 0; i < 5; i++)
    {
        cout << *(i + a) << endl;
    }
    
    return 0;
}

        输出结果如下: 

 

  •  数组访问操作符([ ]
    • 只能通过类的成员函数重载重载
    • 函数能且仅能使用一个参数
    • 可以定义不同参数的多个重载函数
#include <iostream>
//#include <string>

using namespace std;

class Test
{
    int a[5];
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];
        }
        else if (s == "4th")
        {
            return a[3];
        }
        else if (s == "5th")
        {
            return a[4];
        }
    }
    
    int length()
    {
        return 5;
    }
};

int main()
{
    Test t;
    
    for (int i = 0; i < t.length(); i++)
    {
        t[i] = i;
    }
    
    for (int i = 0; i < t.length(); i++)
    {
        cout << t[i] << endl;
    }
    
    cout << endl;
    
    cout << t["5th"] << endl;
    cout << t["4th"] << endl;
    cout << t["3rd"] << endl;
    cout << t["2nd"] << endl;
    cout << t["1st"] << endl;
    
    return 0; 
}

        输出结果如下: 

         这个示例说明可以将字符串作为下标访问数组。


         所以之前写的数组类的代码可以进一步完善啦:

        IntArray.h:

#ifndef _INTARRAY_H_
#define _INTARRAY_H_

class IntArray
{
private:
    int m_length;
    int* m_pointer;
    
    IntArray(int len);
    IntArray(const IntArray& obj);
    bool construct();
public:
    static IntArray* NewInstance(int length); 
    int length();
    bool get(int index, int& value);
    bool set(int index ,int value);
    int& operator [] (int index);
    IntArray& self();
    ~IntArray();
};

#endif

         IntArray.cpp:

#include "IntArray.h"

IntArray::IntArray(int len)
{
    m_length = len;
}

bool IntArray::construct()
{
    bool ret = true;
    
    m_pointer = new int[m_length];
    
    if( m_pointer )
    {
        for(int i=0; i<m_length; i++)
        {
            m_pointer[i] = 0;
        }
    }
    else
    {
        ret = false;
    }
    
    return ret;
}

IntArray* IntArray::NewInstance(int length) 
{
    IntArray* ret = new IntArray(length);
    
    if( !(ret && ret->construct()) ) 
    {
        delete ret;
        ret = 0;
    }
        
    return ret;
}

int IntArray::length()
{
    return m_length;
}

bool IntArray::get(int index, int& value)
{
    bool ret = (0 <= index) && (index < length());
    
    if( ret )
    {
        value = m_pointer[index];
    }
    
    return ret;
}

bool IntArray::set(int index, int value)
{
    bool ret = (0 <= index) && (index < length());
    
    if( ret )
    {
        m_pointer[index] = value;
    }
    
    return ret;
}

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

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

IntArray::~IntArray()
{
    delete[]m_pointer;
}

        main.cpp: 

#include <iostream>
#include <string>
#include "IntArray.h"

using namespace std;

int main()
{
    IntArray* a = IntArray::NewInstance(5);    
    
    if( a != NULL )
    {
        IntArray& array = a->self();
        
        cout << "array.length() = " << array.length() << endl;
    
        array[0] = 1;
        
        for(int i=0; i<array.length(); i++)
        {  
            cout << array[i] << endl;
        }
    }
    
    delete a;
    
    return 0;
}

         输出结果如下:

 

三、小结

  • string 类最大程度的兼容了 C 字符串的用法
  • 数组访问符的重载能够使得对象模拟数组的行为
  • 只能通过类的成员函数重载数组访问符
  • 重载函数能且仅能使用一个参数
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风自在 流水潺潺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值