Delegate of Delegate in .NET C ++ simulation

zhaozj2021-02-11  230

Sender: Roachcock (Poor Ghost), Cyre: Programming Title:.

With a template's offsetting and member template, the overload function call operator successfully implements Delegate, which can bind a normal function, or bind object and its member functions compiled under CygnUWIN, and a delegate contains The use of multiple functions, but I believe it is very simple, from std :: list, you can use a class of Cygun, MY_DELEGATE D2 = My_DELEGATE (T, & Test :: f); ^ If writing & t, it will result in The internal error, there is no way, I have written the program to be empty, and I have not been put on the BBS. I have never endured the various calls in Win32. I have no consideration, but I don't have it. It is trouble, no matter // test.cpp: defines the entry point for the console application. // #include template

// Function Traits, used to extract the return type of the function

struct function_traits {}; template struct function_traits {typedef RT result_type;}; template struct function_traits {typedef RT result_type ; typedef AT argument_type;}; template struct function_traits {typedef RT result_type; typedef AT1 first_argument_type; typedef AT2 second_argument_type;};

// Function traits, used to extract the return type of class member function

template struct function_traits {typedef OT object_type; typedef RT result_type;}; template struct function_traits {typedef OT object_type; typedef RT result_type; typedef AT argument_type; typedef AT first_argument_type;}; template struct function_traits {typedef ot object_type; typef rt Result_type; typef at1 first_argument_type; type;};

// the class a common function to the function type into a member of the specified class type compatible template struct to_member_function_pointer {}; template struct to_member_function_pointer {Typedef r (ot :: * type) ();}; template struct to_member_function_point {typedef rt (Ot :: * Type (At);}; Template Struct To_member_function_point {typedef rt (Ot :: * Type) (AT1, AT2 );}; Template Struct To_Member_Function_Pointer {typedef RT (Ot :: * Type) (AT1 , AT2, AT3);}; // Translate into Const member functions

template struct to_const_member_function_pointer {}; template struct to_const_member_function_pointer {typedef RT (OT :: * type) () const;}; template struct to_const_member_function_pointer {typedef RT (OT :: * type) (AT) const;}; template struct to_const_member_function_pointer {typedef RT (OT :: * type) (AT1, AT2) const;}; template STRUCT TO_CONST_MEMBER_FUNCTION_POINTER {typedef rt (Ot :: * Type) (AT1, AT2, AT3) const;};

// delegate implementation

template class delegate {class object {} * m_pObject; // pointer to the object is a proxy object typedef typename to_member_function_pointer :: type object_member_fuunction_pointer; union {PFT m_pf; object_member_function_pointer m_pmf;}; // Function pointers and function pointers consortium members public: typedef typename function_traits :: result_type result_type; delegate () {m_pObject = NULL; m_pf = NULL;} delegate (PFT pf) {operator = (pf);} template delegate (OT * pObject, typename to_member_function_pointer :: type pmf) {m_pObject = reinterpret_cast (pObject); m_pmf = * (reinterpret_cast (& pmf));} template delegate (OT & pObject, typename to_member_function_pointer :: type pmf) {m_pObject = reinterpret_cast (& pObject); m_pmf = * (reinterpret_cast (& pmf));} template Delegate (Const OT * POBJECT, T ypename to_const_member_function_pointer :: type pmf) {m_pObject = const_cast (reinterpret_cast (pObject)); m_pmf = * (reinterpret_cast (& pmf));} template delegate (const OT & pObject, typename to_const_member_function_pointer :: type pmf) {m_pObject = const_cast (reinterpret_cast (& pObject)); m_pmf = * (reinterpret_cast (& pmf) } DELEGATE & OPERATOR =

(PFT PF) {m_pf = pf; m_pobject = 0; return * this;} Template : GCC's function template is not parametric, plus a placeholder "int" can pass the result_type operator () () {if (m_pobject -> * m_pmf) (); else return m_pf ();} template result_type operator () (at a1) {if (m_pobject) Return (m_pobject -> * m_pmf) (A1) Else Return M_PF (A1);} template Result_Type Operator () (AT1 A1, AT2 A2) {if (m_pobject) Return (M_PObject -> * m_pmf) (A1, A2); Else Return M_PF (A1, A2);} Template Result_Type Operator () (AT1 A1, AT2 A2, AT3 A3) {if (m_pobject) Return (m_pobject -> * m_pmf) (A1, A2, A3); ELSE RETURN M_PF (A1, A2, A3);}}; int GF (int) {RETURN 0;} class test {public: int F (int) {return 0;}}; typedef delegate my_dlegate; int main () {test t; my_dlegate d1 = & gf; // ordinary function my_delegate D2 = my_dlegate (t, & test :: f); // Objects and class members function D1 (0); // call D2 (2);} - If you don't have money, you have no

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

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.171, SQL: 9