Overload multi-dimensional array subscript C ++

Source: Internet
Author: User

actually, the two [] are superimposed. For more information, see Code .

# Include <iostream> <br/> # include <string> </P> <p> using namespace STD; </P> <p> template <class T> class arr; <br/> template <class T> class arrbody <br/>{< br/> PRIVATE: <br/> friend class arr <t>; <br/> T * data; <br/> int row, Col, current_row; <br/> arrbody (int r, int C, t d): Row (R ), COL (c) <br/>{< br/> DATA = new T [R * C]; <br/> current_row =-1; <br/> for (int K = 0; k <r * C; k ++) // initialize data. The default value is 0. <br/> data [k] = D; <br/>} <Br/> Public: <br/> T & operator [] (Int J) // reload the 2nd [] No. <br/>{< br/> If (j> = 0 & J <col) <br/> return data [current_row * Col + J]; <br/>}< br/> ~ Arrbody () {Delete [] data ;}< br/> }; </P> <p> template <class T> class arr <br/>{< br/> PRIVATE: <br/> arrbody <t> tbody; <br/> Public: <br/> arrbody <t> & operator [] (int I) // reload the first [] No. <br/>{< br/> if (I >=0 & I <tbody. row) <br/> tbody. current_row = I; <br/> return tbody; <br/>}< br/> Arr (int I, Int J, t d = 0): tbody (I, j, d) {}< br/>}; </P> <p> void main () <br/>{< br/> arr <int> A (10, 20 ); <br/> arr <double> B (5, 5); <br/> cout <A [5] [5] <Endl; <br/>}< br/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.