C++11. Use a delegate constructor. and high-speed variable initialization, Defaultkeyword re-declares the default constructor to answer the pod status.
Analyze and recommend the method.
Up to now, the VS2012 and 2013 exception declarations are compatible or stay at the communication code level, checking that occurrences such as the following error can be ignored.
Warning C4290: Ignores C + + exception specification, but indicates that the function is not __declspec (nothrow)
Below: VS2012 does not support the delegate constructor. It is recommended to update vs to version 2013 for friends with Cocos2d-x 3.2 and release number.
1>D:\CPP_LAB\TESTQUEUE_16TH_2\TESTQUEUE_16TH_2\HANDLE.HPP (9): Error C2614: "handle<t>": illegal member initialization: "Handle <int> "is not a base or member
At the same time, VS2012 also does not support high-speed initialization of variables.
VS2013 the same time. for the default declaration. However, if you consider using the delegate constructor together, there will be an error such as the following:
Handle () = Default:handle (P,new std::size_t (1)) {};
1>D:\CPP_2013\TESTQUEUE_16TH_2\TESTQUEUE_16TH_2\HANDLE.HPP (7): Error C2143: syntax error: Missing ";" (In front of ":")
There is semantic semantics of two at compile time. Do you want to define the semantics of the form again ():()?
The reason: Once you use Defaultkeyword to redefine the default constructor to revert to the Pod state, remember that the function body cannot be defined, and the default constructor is synthesized by the compiler.
If the function body of the default constructor is defined, a compilation error that is not recognized by the overload definition occurs.
VS2013 version source code such as the following:
Class Handle{public:<span>handle () = default;//cannot declare Function body </span>handle (T *p = nullptr): Handle (p,new std:: size_t (1)) {}handle (const Handle & H): Handle (H.m_pptr,h.m_pusecount)//delegate constructor, the target constructor is called first, and then initialization {}handle & operator = (const Handle & other); T & operator * () throw (Std::runtime_error); T * operator () throw (Std::runtime_error); T & operator * () const throw (std::runtime_error); T * operator () const throw (std::runtime_error); ~handle (void) {Rem_ref ();} Private:handle (t*p,size_t * use): M_pptr (P), M_pusecount (use)//target constructor {if (m_pptr==nullptr) {delete m_pusecount; m_ Pusecount = nullptr;}
<span style= "font-family:arial, Helvetica, Sans-serif;" ><span></span>else</span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ><span></span>{</span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><span style= "font-family:arial, Helvetica, Sans-serif;" >++ (*m_pusecount);</span>
</pre><pre name= "code" class= "CPP" ><span style= "font-family:arial, Helvetica, Sans-serif;" ><span></span>}</span>
};void Rem_ref () {if (--*m_pusecount = = 0) {Delete M_pptr;delete m_pusecount;m_pptr = Nullptr;m_pusecount = nullptr;}} Private:t * M_pptr = nullptr;//Actually this assignment doesn't mean much <span></span>size_t * m_pusecount = nullptr;}; Template <typename t>handle<t> & handle<t>::operator = (const handle<t> & other) {+ + Other.m_pusecount;rem_ref (); m_pptr = Other.m_pptr;m_pusecount = Other.m_pusecount;return *this;} Template <typename t>inline T & Handle<t>::operator * () {if (m_pptr!=nullptr) {return *m_pptr;} Else{throw Std::runtime_error ("Dereference of Unbound Handle");}} Template <typename t>inline T * Handle<t>::operator () {if (m_pptr!=nullptr) {return m_pptr;} Else{throw Std::runtime_error ("Dereference of Unbound Handle");}} Template <typename t>inline T & Handle<t>::operator * () const{if (m_pptr!=nullptr) {return *m_pptr;} Else{throw Std::runtime_error ("Dereference of Unbound Handle");}} Template <typename t>inline T * handle&Lt T>::operator () const{if (m_pptr!=nullptr) {return m_pptr;} Else{throw Std::runtime_error ("Dereference of Unbound Handle");}}
Main function:
int*p = new int (100); Handle<int> pInt (P); handle<int> PInt2 = handle<int> (pInt); handle<int> PInt3 = pint2;try{cout << *pint << "\ t" << *pint2 << "\ t" << *pint3 <&L T Endl;} catch (Runtime_error e) {cerr<<e.what () <<endl;} Try{*pint3 = 200;cout << *pint << "\ t" << *pint2 << "\ t" << *pint3 << Endl;} catch (Runtime_error e) {cerr << e.what () << Endl;} int i = 0;try{pint.~handle (); cout << ++i << "\ t" << *pint << "\ t" << *pint2 << "\ T" &l t;< *pint3 << endl;pint.~handle () cout << ++i << "\ t" << *pint << "\ t" << *pint2 &L t;< "\ t" << *pint3 << Endl;} catch (Runtime_error e) {cerr << e.what () << Endl;} Try{pint.~handle () cout << ++i<< "\ t" << *pint << "\ t" << *pint2 << "\ t" << *pin T3 << ends; /* The previous + + operation was run, but an exception occurred. Exception destruction only destroys and processes variables. Does not roll back the operation */}catch (Runtime_error e) {cerr << i << "\ T" << e.what () << Endl;}
The use of a delegate constructor is to define a constructor that internally defines a full version number, and then call the constructor when the class object is initialized, before the current address is finished. This thought should have been the use of this for a number of times (in fact I have not used a lot of pointers in the form of this), and later the new standard definition This can only be a const type, fused with the current code is a large number of constructors. Thinking out of the solution is not a grammatical breakthrough or innovation.
Program results:
Source
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
C++11 Implementing template handles: Delegate constructors, Defaultkeyword analysis