type class-name::operator[](int index) { //operations }
e.g.:
const int SIZE=5; class arraytype{ int a[SIZE]; public: arraytype(){ int i; for(i=0;i<SIZE;i++) a[i]=i; } int operator[ ](int i){return a[i]; } }; int main( ) { arraytype ob; int i; for(i=0;i<SIZE;i++) cout<<ob[i]<<" "; return 0; }
Liked By
Write Answer
How to overload subscript operator[ ] in c ?
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Join MindStick Community
You have need login or register for voting of answers or question.
AVADHESH PATEL
15-Jun-2013