Non-discussion MFC (3) Quality issues of library code

zhaozj2021-02-16  64

Non-discussion MFC (3) Quality issues of library code

Keywords: C , MFC, Rect, CRECT, POINT, CPOINT, Quality

Explanation: The program is broken only includes the code necessary to understand, and the rest is omitted.

Everyone's code is impossible to completely exclude quality hidden dangers, but MFC is used as a library code, and it will not be excessive.

1. Take the efficiency File: // in TypedEf struct tagRect {long left; long top; long right; long bottom;} Rect, * Prect, Near * npRect, Far * LPRECT; File: // in < AFXWIN.H> CPOINT & TAGRECT (); cpoint & list (); cpoint & bottomright ();}; file: // in _afxwin_inline cpoint & cRect :: Topleft () {return * ((cpoint *) this } File: // [1] _AFXWIN_INLINE CPOINT & CRECT :: Bottomright () {Return * ((cpoint *) THIS 1);} file: // [2] TOPLEFT () By returning cpoint & simultaneous SET and GET Function, and, return CPOINT & comparison to return CPOINT efficiency. However, the implementation of the function must rely on the spanning transformation of the pointer (that is, converted from crect * to a completely unconfilled cpoint *), but also assume that the compiler is sequentially stored in the data member. Free conversion pointer type, unsafe; dependent compiler is implemented, not portable; after the expansion, it can be maintained in maintenance (such as adding data members), and it is possible to cause errors (such as introducing virtual functions, some compilers put the virtual table At the front of the object storage address.

2. Do not care about the efficiency File: // in Class CRECT: PUBLIC TAGRECT {Bool PtinRect (Point Point) const;}; because the Point structure is greater than the 32-bit address length, the use value is not high, it should Change to reference. The software design should maintain a unified gain principle. If it is in the previous point, don't hesitate to improve the efficiency, then you can make reasonably improve the efficiency where you want to let?

3. Algorithm is not rigorous file: // in Class CRECTE: Public tagRect {bool isRectempty () const;}; isRectempty () function function is to return 1 when the rectangular area is empty; when the rectangular area is not Returns 0 times. Give the following test code: CRECT RCT (100, 100, 0, 0); BOOL B = rct.isrectempty (); after running B is actually 1! ? Some CRECT members functions such as intersectRect (), unionRect (), etc., only call NormalizeRect () to ensure that the correct result is obtained. But isRectempty () is completely unnecessary dependent (), for example, can implement: BOOL CRECT :: ISRECTEMPTY () const {return (left == Right && Up == BOTTOM? 1: 0);} Pi-MFC implementation Yes: If the rectangular Right <= Left or Bottom <= Up returns 1.4. Ruleless rules for no reason FILE: // in Class CRect: public tagRect {void operator = (const reference & srcRect); Void Operator = (lpcRect LPRECT);}; Custom Type Don't be worthless of the built-in type is not compatible ("Effective C " language). Operator = () should return CRECT &, so you can also support chain assignment. Similarly Operator = () should also return CRECT &.

5. No effort to ensure security file: // in Class CRECT: Public tagRect {CRECT OPERATOR (LPCRect LPRECT) const;}; operator () should return const cret, doing so if you do not shape (A b) = C; the pathological statement, but also maintains the consistency of the built-in type.

6. No good quality and reliability file: // in class cdc: public cobject {bool bitblt (int X, int y, int nwidth, int nheight, cdc * psrcdc, int xsrc, int ysrc, DWORD DWROP);}; be a simple class ratio: file: // in size_t __cdecl strlen (const char *); Why is the Const Char *? Because of the one, const char * can accept both constant strings and accept very quantitative strings, while char * can only accept very quantitative strings. Second, const can guarantee that the function does not change the contract of the original string. So in the bitblt () statement, the parameter PSRCDC is the original device environment, which does not change, should be declared as const cdc *.

Please refer to the previous "unfinished MFC (2) logic."

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

New Post(0)