Read defective C ++
If you do not know the harms of using soldiers, you cannot know the benefits of using them-Sun Tzu's Art of War
1. Why do APIs use C instead of C ++?
In the past, it has been quite strange why most APIs are provided in C mode. Even if there is a C ++ interface, C functions are only packaged into another layer. Since everyone is using the C ++ compiler, why not directly provide C ++ APIs? I have made some attempts to compile C ++ DLL, but it is quite troublesome. It seems that compiler support is not good. In fact, the core reason is that the private members of the class must appear in the header file. Every time a private member is modified, all modules that use the DLL must be re-compiled. This means that the DLL of the C ++ API cannot be updated independently. In addition, the user of the api library must use the same compiler compilation as the api library, or be specially designed to be compatible with its DLL Compiler (such as the previously Borland C ++ MFC compatibility mode ). Therefore, most APIs use either C or com-type pure interfaces.
2. Why c ++ProgramMostly do not use exceptions
Because c ++ adds the exception function late, most programs use the c Return Error code method. TheseCodeIt is not an exception-safe code, because they cannot ensure that resources such as memory are correctly released when an exception is thrown. When an exception is thrown, the system jumps to the nearest catch statement. Subsequent resource release code will not be executed. To ensure exceptional security, all heap allocation object pointers must be packaged using smart pointer objects such as smart_ptr or shared_ptr, or the release should be controlled by using an extended try-finally statement. While most programmers are reluctant to introduce more troubles, so they simply don't need exceptions. However, this also brings about some problems. constructor and reload operator functions cannot return error codes, so they can only be required to ensure success. Therefore, the constructor does not give any disciplinary advice on resource allocation. However, even if an exception is used, the constructor running error cannot be completely solved. As specified in C ++, constructor fails and the constructor does not execute. That is to say, the resources allocated before the constructor throws an exception cannot be released.