| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 564 人关注过本帖
标题:如何重载二维数组
只看楼主 加入收藏
ownmeanslost
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2015-3-17
结帖率:0
收藏
 问题点数:0 回复次数:0 
如何重载二维数组
定义一个数组类Array,有一个二维数组作为数据成员,分别重载下标运算符[]和函数调用运算符(),其中,函数调用运算符()用来判断二维数组的下标是否越界。
我会用一维数组重载operator[];但是不会重载二维数组运算符operator[][]
#include <iostream.h>
#include <stdlib.h>      
class Array
 {
    public:
        Array();
        int operator[](int index);
        bool operator()(int index);
    private:
        int a[10];
   
 };

 Array::Array()
 {
    int i;
    for(i=0;i<10;i++)
   
        a[i]=0;
 }

 int Array::operator[](int index)
{
    bool b=operator()(index);
    if(b) return a[index];
    else
    {
        cout<<"下标越界了"<<endl;
        exit(1);
    }
}
bool Array::operator()(int index)
{
    return index>=0&&index<10?true:false;
}

int main()
{
    Array obj;
    cout<<obj[10]<<endl;
    //cout<<obj<<endl;
    return 0;
}
搜索更多相关主题的帖子: private include public 如何 
2015-04-26 18:39
快速回复:如何重载二维数组
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015850 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved