# Overload subscript operator [] in C ++

## The subscript operator [] in C ++ is often used to access array elements. This operator can be overloaded to enhance existing functionality in arrays in C ++.

The subscript operator [] in C ++ is often used to access array elements. This operator can be overloaded to enhance an existing feature of arrays in C ++ (so it can be called an array index operator).

The following example illustrates how to load the subscript operator [] in C ++:

` #include using namespace  std ; const int  KICHCO  = 15 ; class ViDuMang { private : int  mang [ KICHCO ]; public : ViDuMang () { register int  i ; for ( i  = 0 ;  i  <  KICHCO ;  i ++) {  mang [ i ] =  i ; } } int & operator []( int  i ) { if (  i  >  KICHCO  ) {  cout  << "n======================n" << endl ;  cout  << "Chi muc vuot gioi han!!" << endl ; // Tra ve phan tu dau tien. return  mang [ 0 ]; } return  mang [ i ]; } }; int  main () { ViDuMang  V ;  cout  << "Gia tri cua V la: " <<  V [ 3 ] << endl ;  cout  << "Gia tri cua V la: " <<  V [ 6 ]<< endl ;  cout  << "Gia tri cua V la: " <<  V [ 16 ]<< endl ; return 0 ; } `

Compiling and running the above C ++ program will produce the following results: Picture 1 Overload subscript operator [] in C ++ download this picture HERE

According to Tutorialspoint

Previous article: Load the operator stack call the function () in C ++

Next lesson: Overload member access operator (->) in C ++
{"ft_lang_label":"__label__en","ft_lang_prob":0.54643285,"math_prob":0.68928856,"size":1129,"snap":"2019-51-2020-05","text_gpt3_token_len":325,"char_repetition_ratio":0.14666666,"word_repetition_ratio":0.060728744,"special_character_ratio":0.38175377,"punctuation_ratio":0.15048544,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9858886,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-19T02:54:31Z\",\"WARC-Record-ID\":\"<urn:uuid:f68af94b-5087-4b33-b40b-0748b8539689>\",\"Content-Length\":\"30781\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4721d061-4cff-48d0-b2fc-5d517e0784de>\",\"WARC-Concurrent-To\":\"<urn:uuid:0d5caa01-5b4c-4e2e-8a41-7d3b333b0803>\",\"WARC-IP-Address\":\"167.114.64.218\",\"WARC-Target-URI\":\"https://tipsmake.com/overload-subscript-operator-%5B%5D-in-c-\",\"WARC-Payload-Digest\":\"sha1:T4J3ZNFWD7IT7XSQSFR2SUJ2HMGVT3JF\",\"WARC-Block-Digest\":\"sha1:VKQJ452222MY7H6KPDVWLOWBIUIP54C6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250594101.10_warc_CC-MAIN-20200119010920-20200119034920-00289.warc.gz\"}"}