How often is the class in C ++ to avoid unnecessary recompilation

xiaoxiao2021-03-06  45

In a larger program, there is always one or two basic classes to be referenced by most of the other programs, and other programs may be dozens or hundreds of CPPs. Whenever the basic class is a small change, the relevant files All needs to re-compile, if the transfer is the public interface, that is not, but in fact, if the design is reasonable, the change is not a public function, but it is necessary to add a private function, or the private member variable. This is not worth it. Taking me as an example, my current work is to maintain several large control procedures for the company. The procedures have almost 100,000 lines of code, and the documents are almost nearly 100, which often needs to change a bug, most of them. Add some private members or private methods. Then, other files are all recompiled, but it is ok, my computer speed can also be CPU 2.8G memory 1G.

However, two days saw the third chapter of C programming ideas, mention this concept of the handle, carefully thinking, in fact, he can improve the above situation and make it very simple.

The solutions mentioned in the book are: the interface that needs to be disclosed is encapsulated by a handle, and the caller is called only by the packaged handle class, so that the call to the implementation is separated, or only let the call to see The interface it needs to be seen. In this case, as long as the interface is not changed, the function of implementing the code is added, the function will not affect the caller.

Ok, this is indeed what I need. The principle is so simple, the implementation is simpler, see below I will write an example.

//Cm_sheetinterface.hclass cm_sheet; // The specific implementation class declaration is enough for generating the Handle pointer. Class cm_sheetinterface {// packaged handle private: cm_sheet * handle; public: cm_sheetInterface (); ~ cm_sheetinterface ();

Void Draw () {handle-> Draw ();} void save () {handle-> save ();} ....

//MyApp.cpp calling code #include "CM_SheetInterface.h" CM_SheetInterface :: CM_SheetInterface () {handle = new CM_Sheet ();} CM_SheetInterface :: ~ CM_SheetInterface () {delete handle;} ... CM_SheetInterface m_Sheet; m_Sheet.draw (); M_sheet.save (); ...

//Cm_sheet.hcalss cm_sheet {private: ... public: cm_sheet () {...} ~ cm_sheet () {...} void Draw (); void save (); ...}

//Cm_sheet.cpp ...

Really implement the cm_sheet class, you can add it to its private member private function to delete and delete it, but will not affect the call code, because the calling code contains the handle cm_sheetInterface (I named it INTERFACE, because it is actually true Very like a COM's interface, oh, as long as the cm_sheetinterface's header file is unchanged, the caller does not need to recompile.

It is really simple. Hey.

Note: When the handle is called, we use inline to reduce the loss of calls during the call. Of course, this practice also has a disadvantage: all public interfaces need to be written twice, if the design is very perfect, just in the first Copy, if you often change the public interface, I want to design itself.

The above is just that I have seen the existing work when I see a third chapter (handle class) in C programming ideas and Appendix C (analog fiction creation functions). It doesn't necessarily have any practical purposes, just think of it. If there is something wrong, please correct it.

2004-11-30

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

New Post(0)