重载操作符[]

代码

#include <iostream>
using namespace std;

template<int Size>
class Index
{
    public:
        int operator[](char vchar) 
        { return vchar % Size; }
};

int main()
{
    Index<26> index;
    cout << index['a'] << endl;
}

结果

19

注意

模版分类型模版(函数模版、类模版)和非类型模版,这里是非类型模版,在使用时注意在<>声明此参数

posted @ 2014-03-14 22:28  jihite  阅读(254)  评论(0编辑  收藏  举报