标签:
#include <iostream> #define DefaultSize 10 using namespace std; template<typename T> class Grail { public: Grail() { data = new T[DefaultSize]; for(int i=0;i<10;i++) { data[i]=i; } } const T& operator[](int size)const { return data[size]; } T& operator[](int size) { return data[size]; } private: T *data; }; int main() { const Grail<int> G1; cout<<G1[3]<<endl;//调用const operator[] Grail<int> G2; cout<<G2[3]<<endl;//调用operator [] return 0; } //编译器能够根据对象的const属性区别对operator和const和非const版本的调用.
标签:
原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/45191007