Template and abnormal problems

xiaoxiao2021-03-06  66

Template and abnormal problems

Seeing this problem on 9CBS (text is http://community.9cbs.net/expert/topic/3565/3565571.xml?temp =.4035608):

There is a code as follows, help fill in 5 space ~~~~~~ 01 #include 02 Template class array; 03 04 template class arraybody {05 friends __ (1) __; 06 T * TPBODY; 07 IROWS, ICOLUMNS, ICURRENTROW; 08 ArrayBody (int IRSZ, ICSZ) {09 TPBODY = __ (2) __; 10 irows = IRSZ; ICOLUMNS = ICSZ; icrRenTrow = -1: 11} 12 13 public : 14 T & Operator [] (int J) {15 BOOL ROW_ERROR, Column_Error; 16 row_error = column_error = false; 17 try {18 IF (icrrentrow <0 | iburrentrow> = irows) 19 Row_ERROR = true; 20 IF (j < 0 || J> = IColumns) 21 Column_ERROR = true; 22 if (Row_error == True || Column_ Error == True) 23__ (3) __; 24} 25 Catch (CHAR) {26 IF (Row_ERROR == True ) 27 CERR << "Dow Trend [" << icrrentrow << "]"; 28 IF (Column_ERROR = true) 29 CERR << "column subscript [" << j << "]"; 30 cout << "/ n"; 31} 32 Return TPBODY [ICURRENTROW * ICOLUMNS J]; 33} 34 ~ ArrayGody () {delete [] tpbody;} 35}; 36 37 Template Class Array {38 arraybody < T> tbody; 39 public: 40 a RrayBody & Operator [] _ ​​4; 43} 44 array (int IRSZ, ICSZ): __ (5) __ {} 45}; 46 47 void Main () 48 {49 Array A1 (10, 20); 50 Array A2 (3, 5); 51 INT B1; 52 Double B2;

53 b1 = a1 [-5] [10]; // has a proceedment prompt: row down the border [-5] 54 b1 = a1 [10] [15]; // has a proceedment note: liner crosions [10] 55 b1 = a1 [1] [4]; // No offshore prompt 56 b2 = a2 [2] [6]; // There is a proceed tip: column subscripts [6] 57 b2 = A2 [10] [20] ; // has a proceedment prompt; the sumbure crosenal [10] list the bid-offline [20] 58 b2 = a2 [1] [4]; // No Crossing Tip 59} After reading the topic, this test template use and Abnormal capture topic.

Sketchy, here there are 2 type templates, one is Template Class Array; the other is Template

From the top down, obviously the template ArrayBody is a member of Array (line 38), and Array may have to access the members of ArrayBody (not affirmation). Temporarily speculate that the Friend of the first nine 5 behavior is Array , and there is not much class template or class used here, and the possibility of filling this is relatively large.

The second empty (line 9) TPBODY is a pointer, give me the initial impression that what should be new, or what is pointing. It is still not sure, first empty.

Look at the third empty, there is a catch (char), obviously 23 lines should be throw ('e'), of course, is not necessarily 'E', as long as it is a char.

The fourth empty is still not known, the fifth space is definitely arraybody (IRSZ, ICSZ). why? Because this is obviously to construct a class (or template) before constructing Array, and Array member template (or class) is only arraybody, it should be yes.

The simulation here is a two-dimensional array. Look at 49 lines array A1 (10, 20); when performing this statement, you should execute the constructor of Array, and before the Array constructor, you should execute first. ArrayBody's constructor, so the first to execute ArrayBody (int IRSZ, INT ICSZ), TBody is a pointer, should it point to, it should be assigned a space, I first thought of the second empty fill in New T IRSZ] [ICSZ]. After allocating a good space, give each member variable. Ok, ok, no problem.

The same is the same as the 50th line of code.

By 53 lines, B1 = A1 [-5] [10]; first execute ArrayBody & operator [] (INT i), then execute T & Operator [] (INT J), and finally returned to the two-digit array specified Variable value. Chapter 23 RTURN TPBODY [ICURRENTROW * ICOLUMNS J]; This can be seen. Strange, how Icurrentrow doesn't seem to be all the first value, right! The fourth empty (line 41) should fill in Tbody.ICURRENTROW.

Ok, slow, returntrow * icolumns j] is a one-dimensional array, the second empty New T [IRSZ] [ICSZ] should be NEW T [IRSZ * ICSZ]. In this way, even the guess is filled. But you can't guarantee its correctness.

It is necessary to verify it now. Take 54 lines B1 = A1 [10] [15];

1. First execute ArrayBody & Operator [] (INT i), at this time, A.TBODY.ICURRENTROW should be 10;

2. Only T & Operator [] (Int J), A.TBody.j should be 15. Due to the value of A.TBODY.ICURRENTROW is equal to the maximum number of rows allow (IROWS == 10, A.TBODY.ICURRENTROW can only be 9), so row_error == true, column_ error == false, Capture exceptions, thus row the crosions. Just as programs prompts. So you should think that this answer is correct.

The above is a personal point of view, and the mistakes respect the advice! ^ _ ^

转载请注明原文地址:https://www.9cbs.com/read-90120.html

New Post(0)