When we do and image-related projects, many cases we will "tailor" an image class. Here is some experience I have summarized in the project. (Note: CIMGWRAPPER as an example) 1. The frequency of the image of Width and Height is high, we should provide an interface to make users easily get them, such as: public: int getWidth () const; int GetHeight () const; 2. In many cases, we need microach map of the image, usually we all construct a miniature diagram object according to an existing image. In fact, the main purpose of our use of miniarts is to display, greatly do not have to construct a miniature map object. We can add two member variables for image classes: private: int cur_width; // Image display size? INT CUR_HEIGHT; then provides interface access and setting these two variables: public: int getCurWidth () const; int getcurheight () Const Void setCurWidth (int NW); Void setcurheight (INT NH); under the Window platform, the following is the code display 1/4 miniartic code: cimgwrapper image; // load image for file image.setcurWidth (image.getwidth () * 0.25); Image.Getcurheight () * 0.25); :: setstretchbltmode (HDC, Coloroncolor); :: StretchDibits (HDC, 0, 0, Image.getcurWidth (), Image.getcurHeight (), 0,0 Image.getWidth (), image.getheigh (), lpbits, lpbitmapinfo, dib_rgb_colors, srcopy; this can be displayed 1/4 microcompillary map. 3. General, the image file should contain two parts: used to describe the image file itself, exist in the form of the header; the information behind the header is the pixel information of the image, is the main information of the image file. . The image class we designed should also contain both parts: private: char * pixeldata; ?? // Pointer CHAR * file headheaderdata ;? // pointing to the header information, the perfect design is not Considering all situations, there is a need to provide an interface to get the original pixel information. Through this interface, the user can implement custom functions: public: char * getpixeldata () {return pixeldata;} 4. Memory allocation of the image class . An image file may occupy a lot of memory. Therefore, the mutual assignment between the image class will occupy a large amount of memory and CPU time, such as: CIMGWRAPPER A, B; // Initalize A, B from Files CIMGWRAPPER C = A B; Analyzed the above code: Created A and B objects, A B will generate a temporary object to represent A and B and, through this temporary object constructed C. During this process, the system is used to distribute the memory and Release, the efficiency is very low. Generally, masters will use the method of reference to reduce the allocation and release of memory, and the design is more complicated.
I am a rookie, complicated, so I can only use some simple ways, or can be used in a certain occasion: do not allow assignment and copy constructs between objects, can be set by copying the copy constructor and "=" operator. is private: private: CImgWrapper & operator = (const CImgWrapper & rhs); CImgWrapper (const CImgWrapper & rhs); providing a copy function is completed deep copy between the object and the object memory: CImgWrapper * copy (const CImgWrapper * pRhs); 5 a. Default Constructor builds a "null image": cimgwrapper () {pixeldata = null; fileHeaderData = null; cur_width = 0;} The need to provide "null image" is in some operations, so By returning "null image" means a failure. 6. Provides a constructor to load the image file into the memory, and associate with a CIMGWrapper object: CIMGWrapper (const? Char * file); 7. Provide a functions of a load picture, will The picture file is loaded into memory: public: void loading (const char * file); provides an uninstall function, release the memory occupied by the image object, set the image object to "null image": public: void releaseImage () The above is some of the experience I have obtained in the project. As the project is in-depth, there will be more gains, and it may make changes to existing understanding. Note: For those senior programmers who are familiar with OOP, Design Pattern This book is recommended for "Applied C : Practical Techniques for Building Better Software", this book uses a picture class as an example, which expounds a large number of techniques to build excellent software, and uses OOP, Design Pattern and other ideas.