Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the relationship between C++ function pointer and C# delegate

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

C++ function pointer and C# delegate what is the relationship between this, many novices are not very clear, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

There is an application method in C # called delegation. Its application and implementation functions have a lot in common with the C++ function pointers we introduced to you before. In this article, we will make a brief introduction to these two methods to make it easier for you to distinguish and understand.

Both the delegate and the function pointer describe the signature of the method / function and call different implementations through a unified interface. But there is an obvious difference between the two. To put it simply, the delegate object is the real object, and the function pointer variable is only the entry address of the function. For high-level applications, delegation is more flexible and applicable than C++ function pointers, but for underlying applications, function pointers are irreplaceable. Here are examples of delegate type and function pointer type definitions, respectively:

Delegate int Fn (int a, int b) / / C # delegate typedef int (* Fn) (int a, int b) / / C++ function pointer

Formally, the parameter list and return values of both are the same, except that one uses the keyword delegate and the other uses the pointer symbol *. The term "similarity" seems more secure, but it would be too hasty to equate the two immediately. Let's actually verify it and see what the difference is:

/ / C # delegate int Fn (int a, int b); class Adder {private int c = 0; public int Add (int a, int b) {return a + b + c;} public Adder (int c) {this.c = c;}} class Multiplier {private int c = 0; public int Multiple (int a, int b) {return a * b * c;} public Multiplier (int c) {this.c = c;}} Adder adder = new Adder (1) Multiplier multiplier = new Multiplier (2); Fn fn = adder.Add; fn (1,2); / / result is 4 fn = multiplier.Multiple; fn (2,3); / / result is 12

The above code illustrates two problems:

1. Delegate objects can point to different classes of methods, as long as they match the delegate signature

two。 The delegate object is stateful (saved in the pointing object), and the behavior of the delegate is affected not only by the input parameters, but also by the state of the target object.

/ C++ typedef int (* Fn) (int a, int b); int Add (int a, int b) {return a + b;}; int Multiple (int a, int b) {return a * b;}; class Adder {public: Adder (int c) {this- > cc = c;} int Add (int a, int b) {return a + b + c;} private: int c;} Typedef int (Adder::* Fm) (int a, int b); int _ tmain (int argc, _ TCHAR* argv []) {Fn fn = Add; std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report