[] Operator Overloading and [] [] Operator Overloading of two-dimensional array classes

Source: Internet
Author: User

Some Classes in Microsoft's MFC framework, such as cwordarray and cbytearray, provide a set of array classes encapsulated by the object-oriented mechanism, which is very convenient to use and equivalent to variable length arrays in VB. Due to the complete encapsulation, removes the need to allocate memory using the standard C ++ new method, this avoids Memory leakage caused by forgetting the delete memory (the premise is that the instances of these classes are created on the stack rather than on the stack ). Because these classes overload the [] operator, we do not have to use the lame setat () or getat () to access array elements, we can access the elements of the C ** array through square brackets [] Like using an ordinary array. The following code briefly demonstrates the usage:
 
Cstring S;
Cwordarray cwarr;
Cwarr. setsize (1000 );
Cwarr [100] = 90; // This method benefits from the overload of the [] operator.
S. Format ("% d", cwarr [100]); // This method benefits from the overload of the [] operator.
Afxmessagebox (s );

The following statement is displayed in the cwordarray definition in the header file afxcoll. h of Microsoft:

// Overloaded operator helpers
Word operator [] (INT nindex) const;
Word & operator [] (INT nindex );

This is the declaration of operator overloading. The cwordarray code is invisible. However, it is not difficult to simulate cwordarray to write a one-dimensional Word Array class and implement the [] OPERATOR overload. The Code is as follows:

Bytes -------------------------------------------------------------------------------------
// One-dimensional Word Array
Class cwordarr
{
PRIVATE:
Word * m_pdat;
Long m_size;
Public:
Cwordarr (Long SIZE)
{
M_pdat = new word [size];
M_size = size;
};
~ Cwordarr ()
{
Delete [] m_pdat;
};
Word & operator [] (long idx)
{
Return m_pdat [idx];
}
};

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

Operator overloading can be seen as a syntactic modified member function, or as a function call method, which provides syntax convenience (refer: c ++ programming ideas ). Cwarr [100] = 90; this code can be executed because it actually calls word & operator [] (long idx) this member function (called a member function) returns m_pdat [idx] as a word reference. It is equivalent to declaring a reference to m_pdat [idx] temporarily, therefore, the value assignment operation is equivalent to assigning a value to this temporary word reference. The result is that the 90 value is injected into the memory of m_pdat [idx]. After the code is executed, temporary references will be destroyed. This is the operating mechanism of the entire operator overload.

The [] Operator Overloading of one-dimensional array classes is not difficult. Recently, to optimize an algorithm, I wrote a two-dimensional array class myself. Can I overload the [] [] operator, in this way, you don't need to use the getat () setat () membership functions. It wasn't until I read the operator reload chapter in "C ++ programming thoughts" that I was not very familiar with C ++, and my thoughts were naive, c ++ does not directly provide [] [] overloading. Otherwise, it is equivalent to reloading a ternary operator. No ternary operators have been found in C ++, there is no significance for the ternary operators, because the superposition of binary operations can achieve arbitrary meta operations. Similarly, the so-called [] [] overload must also be implemented through the superposition of two [] overloading. Therefore, it is impossible to implement the overload of [] [] In a class. There must be two classes, one being a one-dimensional array class, implementing the overload of [], and the other being a two-dimensional array class, you must hold a group of instances of the one-dimensional array class to store arrays of rows of the Two-dimensional array. The two-dimensional array class also needs to overload the [] Operator. In this way, through the superposition of two [] overloading, you can use a [x] [Y] to access array elements.
The Code is as follows:

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

// One-dimensional Word Array
Class cwordarr
{
PRIVATE:
Word * m_pdat;
Long m_size;
Public:
Cwordarr (Long SIZE)
{
M_pdat = new word [size];
M_size = size;
};
~ Cwordarr ()
{
Delete [] m_pdat;
};
Word & operator [] (long idx)
{
Return m_pdat [idx];
}
};

// Two-dimensional Word Array
Class c2dimwordarr
{
PRIVATE:
Cwordarr ** m_p; // master pointer
Long m_row, m_column; // number of rows, number of columns.

Public:
C2dimwordarr (long row, long column)
{
M_p = new cwordarr * [row];
For (long I = 0; I <row; I ++)
{
M_p [I] = new cwordarr (column );
}
M_row = row;
M_column = column;
}
~ C2dimwordarr ()
{
For (long I = 0; I <m_row; I ++)
{
Delete m_p [I];
M_p [I] = NULL;
}
Delete [] m_p;
}
Cwordarr & operator [] (long idx)
{
Return * m_p [idx];
}
};

Bytes --------------------------------------------------------------------------------------
The main code is as follows to verify the correctness of the class:
Bytes --------------------------------------------------------------------------------------
C2dimwordarr c2dwa (4, 3 );
Long I, J;
For (I = 0; I <4; I ++)
For (j = 0; j <3; j ++)
C2dwa [I] [J] = I * 10 + J;

Cstring S, stmp;
For (I = 0; I <4; I ++)
For (j = 0; j <3; j ++)
{
Stmp. Format ("[% d] [% d] = % d/N", I, j, c2dwa [I] [J]);
S + = stmp;
}
Afxmessagebox (s );
Bytes --------------------------------------------------------------------------------------
This is the code running mechanism. When c2dwa [I] [J] = I * 10 + J; is executed, (c2dwa [I]) first, combine the semantics, execute the [] overload function of the two-dimensional array class, and get a reference of a one-dimensional array. Assume that this reference is called B, and then combine B [J, finally, the reference of m_pdat [idx] is obtained, that is, the actual location of the data stored in the memory is obtained, and the value assignment operation injects the data into the correct memory location.

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.