Template handle implementation, Delegate constructor calls, throw declaration, etc.

Source: Internet
Author: User

C ++ 11: Use a Delegate constructor and quickly initialize the variable. The default keyword declares the default constructor and returns the pod status. Analysis and recommended usage.

So far, vs2012 and 2013 are compatible with exception declarations at the level of code communication, and there is no compilation type check. The following error can be ignored.

Warning c4290: Ignore the C ++ exception specification, but the indicator function is not _ declspec (nothrow)


Under: vs2012 does not support delegate constructors, it is recommended to use cocos2d-x 3.2 and version of friends updated vs to version 2013.

1> D: \ cpp_lab \ testqueue_16th_2 \ testqueue_16th_2 \ handle. HPP (9): Error c2614: "handle <t>": Invalid member initialization: "handle <int>" is not a base or member

At the same time, vs2012 does not support quick variable initialization.



Vs2013 has completed the default declaration at the same time, but if you consider using it with the Delegate constructor, the following error will occur:

<span>Handle() = default{}</span><span style="font-family: Arial, Helvetica, sans-serif;">:Handle(p,new std::size_t(1))</span><span>;</span>

1> D: \ cpp_2013 \ testqueue_16th_2 \ testqueue_16th_2 \ handle. HPP (7): Error c2143: syntax error: ";" missing (before)

Semantic ambiguity occurs during compilation. To redefine the semantics of the = () :() form?

In addition, once the default keyword is used to redefine the default constructor so that it returns to the pod state, remember not to define the function body. The default constructor is synthesized by the compiler. If the function body of the default constructor is defined, compilation errors that cannot be identified by the overload definition will occur.

The source code of vs2013 is as follows:

Class handle {public: <span style = "white-space: pre"> handle () = default; // The function body cannot be declared </span> handle (T * P = nullptr): handle (p, new STD: size_t (1 )) {} Handle (const handle & H): handle (H. m_pptr, H. m_pusecount) // delegate the constructor. The target constructor is called first, and then the initialization is completed {} Handle & operator = (const handle & other); T & operator *() throw (STD: runtime_error); T * operator-> () Throw (STD: runtime_error); T & operator * () const throw (STD: runtime_err Or); 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 style="white-space:pre"></span>else</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"></span>{</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"></span></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 style="white-space:pre"></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; // In fact, this assignment does not mean much <span style = "white-space: pre"> </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 <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 <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" <* pint3 <Endl; pint. ~ Handle (); cout <++ I <"\ t" <* pint <"\ t" <* pint2 <"\ t" <* pint3 <Endl;} catch (runtime_error e) {cerr <E. what () <Endl;} Try {pint. ~ Handle (); cout <++ I <"\ t" <* pint <"\ t" <* pint2 <"\ t" <* pint3 <ends; /* the preceding ++ operation is executed, but an exception occurs. The destruction of the exception only analyzes and processes the variables, and does not roll back the operation. */} catch (runtime_error E) {cerr <I <"\ t" <E. what () <Endl ;}


Program result:


Template handle implementation, Delegate constructor calls, throw declaration, etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.