Many libraries provide several classes, how to tell the caller whether an error occurs when each method goes wrong, and the error code (the global errno in Error.h on Linux is to save the error code that our Linux program executes)? There are many methods, and for the sake of simplicity, the function returns an object that holds the return value and error code of the function.
/* * CLSTATUS.H * * Author:lilin * Email: [email protected] */#ifndef clstatus_h#define clstatus_h//used to save the letter The result of the processing of the number class Clstatus{public:/*lreturncode >=0 indicates success, otherwise the failure */clstatus (long lreturncode, long lErrorCode); Clstatus (const clstatus& s); virtual ~clstatus ();p ublic:bool issuccess ();p ublic:/* by this can be m_lerrorcode and m_ Lreturncode as Public member hidden write, but can publicly read */const long& m_clreturncode;const long& m_clerrorcode;private://return value long m _lreturncode;//error code long M_lerrorcode;}; #endif
#include "CLStatus.h" clstatus::clstatus (long Lreturncode, long lErrorCode): M_clreturncode (M_lreturncode), m_ Clerrorcode (m_lerrorcode) {M_lreturncode = Lreturncode;m_lerrorcode = lErrorCode;} Clstatus::~clstatus () {}clstatus::clstatus (const clstatus& s): M_clreturncode (M_lreturncode), M_clErrorCode (m_ lErrorCode) {M_lreturncode = S.m_lreturncode;m_lerrorcode = S.m_lerrorcode;} BOOL Clstatus::issuccess () {if (m_clreturncode >= 0) return True;elsereturn false;}
where M_lreturncode; stores the return value, M_lerrorcode stores the error code. Const long& M_clreturncode;const long& M_clerrorcode; By doing so can be m_lerrorcode and M_lreturncode as a public member hidden write, But it can be read publicly. Of course we have other options, the Getmireturncode () method to achieve the same effect, of course, it is felt here by providing a Get method, rather than provide a set method more consistent with the object-oriented data encapsulation characteristics. (See the Code on GitHub
Apuesrc/2/2.7/
The above code, is not also the efficiency can also be optimized?
/* * test.cpp * * Author:lilin * Email: [Email protected] */#include <iostream>using namespace Std;class a{ Public:a () {cout << ' in A (): ' << hex << (long) this << Endl;} A (const a&) {cout << ' in A (const a&): ' << hex << (long) this << Endl;} ~a () {cout << ' in ~a (): ' << hex << (long) this << Endl;} a& operator= (const a& A) {cout << ' in operator=: ' << hex << (long) this << "=" << h Ex << (Long) (&a) << endl;return *this;}; A f () {a a;return A;} int main (int argc, char* argv[]) {A a;a = f (); return 0;}
The result of the code operation is as follows:
In a (): 7fff834e277ein A (): 7fff834e277fin operator=: 7fff834e277e = 7fff834e277fin ~a (): 7fff834e277fin ~A (): 7fff834e277 E
To change the code slightly:
/* * test.cpp * * Author:lilin * Email: [Email protected] */#include <iostream>using namespace Std;class a{ Public:a () {cout << ' in A (): ' << hex << (long) this << Endl;} A (const a&) {cout << ' in A (const a&): ' << hex << (long) this << Endl;} ~a () {cout << ' in ~a (): ' << hex << (long) this << Endl;} a& operator= (const a& A) {cout << ' in operator=: ' << hex << (long) this << "=" << h Ex << (Long) (&a) << endl;return *this;}; A f () {return a ();} int main (int argc, char* argv[]) {A A = f (); return 0;}
Look at the results of the operation:
In ~a (): 7ffff682a68fin ~a (): 7ffff682a68e
Obviously the entire process creates an object less and calls the overloaded = operation less. Efficiency has not been significantly improved. But why is that? Here, let's see what the program does.
Modified code we actually created only one object, which was created in the F () function. In a a=f (), in this line of code, the call is not an assignment operation, but the default copy constructor, where the default copy constructor returns the object's reference directly, so that only one Clstatus object is created. Instead of calling the overloaded assignment operation, instead of invoking the default copy constructor, you can refer to the copy constructor and the assignment operator for differences
So, in order to take into account efficiency and portability, the return value of our function in the future is uniformly returned in Clstatus package, and it is suggested that the code be written in the following way:
Clstatus F () {return clstatus (...);} Clstatus s = f ();
(if you have any questions or suggestions, please contact [email protected])
Linux Environment advanced Programming-error handling (Clstatus)