---
title: "How to overload subscript operator[ ] in c  ?"  
description: "How to overload subscript operator[ ] in c  ?"  
author: "Pravesh Singh"  
published: 2013-06-14  
updated: 2013-06-15  
canonical: https://www.mindstick.com/forum/1115/how-to-overload-subscript-operator-in-c  
category: "visual c++"  
tags: ["visual c++"]  
reading_time: 1 minute  

---

# How to overload subscript operator[ ] in c  ?

Hi,\
How to overload subscript [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server)[ ] in [c++](https://www.mindstick.com/blog/745/array-in-c-plus-plus)?\
Thanks in advance.

## Replies

### Reply by AVADHESH PATEL

Try this:\
**syntax:**\

```
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;  }
```


---

Original Source: https://www.mindstick.com/forum/1115/how-to-overload-subscript-operator-in-c

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
