In C ++ moderate to "delegate"

xiaoxiao2021-03-05  54

First come to 1 paragraph

C will never have two years after the top nowhere

I feel that C is very deep in the beginning.

I feel that I feel profound.

Now it is more skilled, I feel that C is very troublesome, it is especially trouble =. =!

The trouble of C is that there is not enough support for OOP.

Delegation is one of them

There are other more than 1 "fiction" "multi-object polymorphism", etc.

Some ancient programming languages ​​are support for these language features.

But I don't know why it is eliminated.

C doesn't know why you can live now

May be the appearance of the fang

But the above language characteristics C do not support

But whenever we are interested or unintentionally or indirectly realize the above language characteristics

But it is often unbelievable.

If it is supported, it will be very convenient.

Just like someone in the previous mold with polymorphism 1

No matter how it is impossible to support polymorphism

The difference between the master and the rookie is that the master will be in the C . It is still in these languages. Anyway, I am not a master. If I am a master, I will not even find God. Save me.

Entering the topic to C should be called function pointer but the function pointer and member function pointer are distinctive, especially member function pointers, our most needed is the way to call the member function pointer to a normal function pointer is actually Implementing 1 nanopical imitation function LOKI has 1 such nathematical imitation function to use and ordinary function pointer 1 but its actual is a bit complex Boost's nanzim imitation function is not very ideal.

My imitation function is of course a fang shape, but the actual is not complicated. 1 piece code

Template Class Delegate;

Template class delegate // offset {typedef r r;

Class functorIMPL / / / / LOKI1 is all invoke the entrusted member function {public: Virtual r operator () () = 0;

Struct bit12 {unsigned long _value [3];}; bit12 value; // This is a pointer to save objects // member function pointer still can't see virtual functions list

Public: // This constructor may be difficult to understand =. = Template // EZMFN >/ INLINE DELEGATE (PTROBJ P, MEMFN MEMFN) // This is the constructor of the entrusted member function {STRUCT D: PUBLIC FUNCTORIMPL / / Local Class Inherits Since FuctorImpl {D (Ptrobj P, Memfn Memfn) // Estructure Configuration Function: PMEMFN // Delegate Construction Functional {}

PTROBJ POBJ; / / Local Class Data MEMFN PMEMFN; // Object Pointer and Member Function Pointer

R operator () // Implement the virtual function {// call the commissioned member function Return ((* pobj). * Pmemfn) ();}};

Assert (SIZEOF (D) == SizeOf (bit12)); // detects whether the size of the local Class size and the saving data is 1 sample D D (p, MEMFN); // Instantiate local Class

Value = * ((Bit12 *) & D); // The instance of local class Class saves the virtual function list // The type of part is not important, it is important to data}

Template Inline Delegate (Const Fun Fun) // This is the constructor of the commissioned normal function {// Basically and the above construction won number 1 Struct D: public functorImpl {d (fun fun): Pobj (0 ), PFUN (FUN) {}

Void * pobj; fun pfun;

R Operator () () {return ();}};

Assert (SIZEOF (D) == SizeOf (bit12));

D D (FUN);

Value = * ((Bit12 *) & D);

Inline Delegate // Copy Construction Function: Value (Other.Value) {}

Inline R Operator () () {functorImpl * pf = (functorImpl *) & value; return (* pf) ();} // This is the other party // of the entrusted function to convert the value to the value To the virtual base class and then call // As for the original type, it is not important // knows that only the virtual base class is};

Everyone may understand that the local class of Class is difficult to understand that the data type in this partial class is determined by the type of parameters of the nanjected constructor, and the constructor of the normal template class 1 DELEGATE will be automatically generated. All new Class all implementations are saved in this class and then call this invisible class through the virtual base class.

Oh ~ Is it still not understood that everyone is not a matter of understanding, it is difficult to understand the difficulty of understanding the fang shape is not more difficult than you have learned a polymorphism.

Come 1 piece of code

Class c0 {public: void f () {cout << "Call F ()" << endl;}};

Void __f () {cout << "call __f ()" << endl;}

INT _TMAIN (int Argc, _tchar * argv []) {

C0 C; DELEGATE Dele (& C, C0 :: f); dele (); // call the member function of the delegate

Delegate __dele (__ f); __ dele (); // Call a normal function

Delegate __copy (__ dele); // Call the COPY constructor __copy ();

__copy = dele; // assignment

__copy ();

While (1); return 0;}

Oh ~ is used to use a normal function pointer 1

The first time I wrote such an article I hope everyone will advise QQ: 4390121 There are two group 5341856 (exchange shader's group) 436442 (group of exchanges in Guangdong), everyone has time to irry

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

New Post(0)