Let's talk about [] Operator Overloading and [] [] [] Operator Overloading of two-dimensional array classes -- [] [] Another Implementation Method of Operator Overloading

Source: Internet
Author: User
Tags cdata

I have already talked about the reloads of [] [] operators in two articles. I thought I could have done it, but I didn't have it. So I had this third article. When I talked with my friends a few days ago, my friend said that she also wrote a code to solve the same problem and sent the code to me. I will take a closer look at it, and the implementation principle is the same as mine, however, the implementation method is the same as that of me. The code is listed as follows:

Compiling environment: VC ++ 6.0 Project type: Windows console Program

Bytes -----------------------------------------------------------------------------------------

// Operator overload 3.cpp: defines the entry point for the console application.
//

# Include "stdafx. H"
# Include "windows. H"

Class CDATA // class for data storage.
{
PRIVATE:
Word * m_data;
Int m_row, m_column; // number of rows, number of Columns
Public:
Int row_index; // The current row number.
CDATA ()
{
M_data = NULL;
};
Setsize (INT row, int column)
{
If (m_data! = NULL)
{
Delete [] m_data;
M_data = NULL;
}
M_data = new word [row * column];
M_row = row;
M_column = column;
Row_index = 0;
};
~ CDATA ()
{
If (m_data! = NULL)
{
Delete [] m_data;
M_data = NULL;
}
};
Word & operator [] (INT index)
{
Return m_data [row_index * m_column + Index];
}; // Subscript operator
Word * getbuffer ()
{
Return m_data;
};
};

Class c2dimwordarr // two-dimensional Word Array class.
{
PRIVATE:
Int m_row, m_column; // number of rows, number of Columns
CDATA array;
Public:
Setsize (INT row, int column)
{
Array. setsize (row, column );
M_row = row;
M_column = column;
};
CDATA & operator [] (INT index)
{
Array. row_index = index; // record the current row number
Return array;
};
Word * getbuffer ()
{
Return array. getbuffer ();
};
};

Int main (INT argc, char * argv [])
{
Printf ("Hello world! /N ");

C2dimwordarr CWA;

CWA. setsize (); // set the array size: 3 rows and 5 columns.
 
Long I, J;

For (I = 0; I <3; I ++) // traverse rows
For (j = 0; j <5; j ++) // traverses the column
CWA [I] [J] = I * 10 + J; // assign a value to each element

 
For (I = 0; I <3; I ++)
For (j = 0; j <5; j ++)
Printf ("CWA [% d] [% d] = % d/N", I, j, CWA [I] [J]); // print each element

// Small experiment of an element operation.
Printf ("CWA [1] [0] * CWA [1] [2] + CWA [2] [1] = % d/N ", CWA [1] [0] * CWA [1] [2] + CWA [2] [1]); // print each element
 
// A small experiment that exchanges data with C/C ++ standard Arrays
Word a [3] [5];
Memcpy (A, CWA. getbuffer (), 3*5 * sizeof (Word ));
 
For (I = 0; I <3; I ++)
For (j = 0; j <5; j ++)
Printf ("A [% d] [% d] = % d/N", I, j, a [I] [J]); // print each element

Return 0;
}

Bytes -----------------------------------------------------------------------------------------

The advantage of this method is that it uses a one-dimensional buffer to store all the two-dimensional data. Therefore, the storage method is fully compatible with the storage structure of the standard two-dimensional array of VC, only one memcpy can be used to copy data to a standard two-dimensional array. In addition, it saves more memory than the code in the previous two articles. In the previous two articles, each line of the Two-dimensional array is represented by a class, therefore, if you use a two-dimensional array with fewer rows and columns, the memory usage is not very economical. Of course, the two implementation methods have their own advantages. Which of them is better depends on the use environment, which is related to the problems to be solved.

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.