曲径通幽论坛

标题: 重载[ ] 和 ( ) 运算符 [打印本页]

作者: beyes    时间: 2011-8-23 17:37
标题: 重载[ ] 和 ( ) 运算符
[ ] 是数组下标运算符,也是一个二元运算符,重载该运算符的通用形式如下:
[C++] 纯文本查看 复制代码

type clsss-name::operator[ ] (int index)
{
    // 函数内容
}

上面的 index 并不一定是 int 类型,但通常 operator[]() 用来做数组下标运算,所以该参数一般为整数类型。

如一个对象写成 ob[3] ,那么重载函数会转换为 ob.operator[](3) 这种调用形式。

示例程序:
[C++] 纯文本查看 复制代码

#include <iostream>
using namespace std;

const int SIZE = 3;

class atype {
        int a[SIZE];
public:
        atype() {
                register int i;
                for (i = 0; i < SIZE; i++) a = i;
        }
        int operator[](int i) {return a;}
};

int main()
{
        atype ob;

        cout << ob[2] << endl;      

        return 0;
}

函数输出整数 2 。
上面函数中,重载运算符函数 operator[]() 将参数作为数组的下标并返回相应的函数值。可以将 operator[]() 函数的返回类型指定为引用类型,这样函数 operator[]() 就可以用在赋值运算符的左边或者右边。如下程序所示:
[C++] 纯文本查看 复制代码

#include <iostream>
using namespace std;

const int SIZE = 3;

class atype {
        int a[SIZE];
public:
        atype() {
                register int i;
                for (i = 0; i < SIZE; i++) a = i;
        }
        int &operator[](int i) {return a;}
};

int main()
{
        atype ob;

        cout << ob[2] << endl;

        ob[2] = 18;     //ob[2] 放在运算符 = 的左边

        cout << ob[2] << endl;

        return 0;
}

运行输出:
$ ./reloadsqub2
2
18
程序中,operator[]() 返回的是下标为 i 的数组元素的引用,它可以用在复制运算符的左边以修改数组中元素的值,当然也可以放在右边用来给一个变量赋值。

在 C++ 中不会主动对数组边界溢出做检查,这可能会带来错误的操作。比如在创建的一个包含数组的类,只允许通过重载的下标运算符 [ ] 来访问这个数组,那么就可以截获超出数组边界的下标。示例程序:
[C++] 纯文本查看 复制代码


#include <iostream>
#include <cstdlib>
using namespace std;

const int SIZE = 3;

class atype {
        int a[SIZE];
public:
        atype() {
                register int i;
                for (i = 0; i < SIZE; i++) a = i;
        }
        int &operator[](int i);
};

int &atype::operator[](int i)
{
        if(i < 0 || i > SIZE-1) {
                cout << "\nIndex value of ";
                cout << i << " is out-of-bounds.\n";
                exit(EXIT_FAILURE);
        }
        return a;
}

int main()
{
        atype ob;

        cout << ob[2] << endl;

        ob[2] = 18;
        cout << ob[2] << endl;

        ob[3] = 44;     //产生运行时错误,下标3超出数组边界

        return 0;
}

运行输出:
$ ./reloadsqub3
2
18

Index value of 3 is out-of-bounds.


可以对 ( ) 运算符进行重载,重载 ( ) 运算符并不是创建了一种新的调用函数方法,而只是创建了一个 operator 函数。下面是示例程序:
[C++] 纯文本查看 复制代码

#include <iostream>
using namespace std;

class three_d {
        int x, y, z;
public:
        three_d() { x = y = z = 0; }
        three_d(int i, int j, int k) { x = i; y = j; z = k; }
        three_d operator()(int a, int b, int c);
        void show();
};

//重载运算符()
three_d three_d::operator()(int a, int b, int c)
{
        three_d temp;

        temp.x = x + a;
        temp.y = y + b;
        temp.z = z + c;

        return temp;
}

void three_d::show()
{
        cout << x << ", ";
        cout << y << ", ";
        cout << z << "\n";
}


int main()
{
        three_d ob1(1, 2, 3), ob2;

        ob2 = ob1(10, 11, 12);  //调用 operator() 函数

        cout << "ob1: ";
        ob1.show();

        cout << "ob2: ";
        ob2.show();

        return 0;
}

运行输出:
$ ./reloadbk
ob1: 1, 2, 3
ob2: 11, 13, 15
在重载 ( ) 运算符时,可以自定义任意类型以及任意数量的参数,并且函数的返回值也可以是任意类型的。




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2