Non-discussion MFC (2) logic is not complete
Keywords: C , MFC, Rect, CRECT, POINT, CPOINT, Logic
Explanation: The program is broken only includes the code necessary to understand, and the rest is omitted.
1. Design missing file: // in typef struct tagRect {long left; long top; long right; long bottom;} Rect, * prect, Near * npRect, Far * LPRECT; File: // in < AFXWIN.H> Class CRECT: Public tagRect {void swaplefTright (); file: // [1]
Bool isRectempty () const; bool isrectnull () const; void setRectempty (); file: // [2]
CRECT (INT L, INT T, INT R, INT B); CRECT (Point Topleft, Point Bottomright); CRECT (Point Point, Size Size); File: // [3] Void SetRect (int X1, int y1, int y1, int X2, int y2); void setRect (Point Topleft, Point Bottomright);}; [1] Why is SwapleFTright (), does not provide corresponding SwaptopBottom ()? [2] Why not provide setRectnull ()? [3] Since the three methods can construct CRECT objects, is it not very reasonable to setRect (Point Point, Size Size? It is nothing to do with these missing functions, "It's not because it is good and not". This sentence should not only hang it!
2. No consistency before and after: // in Class Cpoint: public tagpoint {crect operator (const reference * lprect) const; file: // [1]}; typedef const * lpcRect; Class CRECT: Public TagRect; {CRECT OPERATOR (LPCRect LPRECT) Const; File: // [2]
Void Operator = (lpcRect LPRECT); file: // [3] void operator & = (const); file: // [4]}; due to the type defined by LPCRect, [1] [2] Participate in the form of a different form of declarations that have the same meaning. [3] [4] is a similar operator overload, but uses different ginseng delivery. Everyone can have their own code style, but in the same file, or at least in the same class, always use a unified style!
3. Exhibit the integrity of syntax integrity File: // in Class CRECT: Public tagRect {void operator = (const reference & srcRect);}; well known, in C and C , any expression of any expression is Value, for example: A = 100 is an expression, its value is 100. With this logic premise, chain expressions can reasonably exist. In b = a = 100;, it is accurately, it is to assign a value 100 of the expression of A = 100 to B. In the CRECT class, the return type of the assignment operator is incorrectly set as a VOID, so that the assignment expression between the CRECT object does not have a value, the chain expression is also invalid. The correct return type of this operator should be CRECT &. Does MFC developers do not look at "Effective C "? 4. Symmetry of mathematical operation File: // in typedef struct tagpoint {long x; long y;} Point, * Ppoint, Near * Nppoint, Far * LPPOINT; File: // IN CLASS CPOINT: Public tagpoint {cpoint operator (Point Point) const;}; give the following test code: Point Pt; cpoint pnt; cpoint result; result = Pnt Pnt; file: // okresult = Pt Pt; File : // ErrorResult = Pnt Pt; file: // okresult = Pt Pnt; file: // errorpnt Pnt Nature is no problem, PT PT error can also be understood, but PNT PT can, PT PNT is biased Not. Initially, the addition should satisfy the exchange rate, but the MFC "shocked" broke our thinking inertia. In fact, if the operator function is declared as: Friend Const Cpoint Operator (Const Point & PNTL, Const PNT & PNTR); the foregoing four statements can pass. Maybe someone will say: "Friend keyword is non-objective, it is best not to use." So, I want to say: First, C is not Java, its main design principle is to meet the efficiency, elasticity and maintainability of large systems, Object-oriented methods should be adopted, non-objective methods should also be adopted. Second, MFC uses Friend elsewhere. If it is considered to be added between PNT and PT, it should also declare the operator function as a safer form: Const Cpoint Operator (const cpoint & pntr) const; this is only allowed to add PNT and PNT. Don't do it, you can't do it, don't discriminate against!
Please refer to the next article "Quality of MFC (3) Code"