The true face of the commission in my Eyes (Ii.)

Source: Internet
Author: User

For the last essay, many friends left a very good opinion, but also a lot of friends put forward a very representative problem. So, before the text begins, I would like to set out my own point of view on these issues, and ask my friends to give criticism:

First of all, Feiyang friend proposed the relationship between the delegate and function pointers. I quite agree with this statement.

In fact, the understanding of C + + friends will know: A class contained within the method, in fact, in the class only as a function pointer, its implementation part does not occupy the memory space of the class. If we declare a delegate instance object within a class, it also mounts a method inside other classes (of course, the method must be common), so that within this class there is also a function pointer to the method (which can be understood to be stored within the delegate object)-- This is equivalent to declaring an identical method within this class.

In fact, if we invoke the corresponding method through the declared delegate object, the C # compiler will automatically treat it as a declarative method of this class (the argument is justified from the C # compiler's security perspective). In fact, the target delegate object is not a real class method, but only as a target method of proxy method exists, this is the origin of the term of the principal (agent).

In addition, Feiyang also mentioned the difference and connection between the delegation and the interface callback mechanism, and I have given a reply in the previous essay about some limitations of the interface callback method. Here, we might try to use the interface callback to implement the C # event mechanism:

5 (2) The event mechanism that uses an interface to callback the emulation control (attempt)

First, we set up a control project with the following code:

namespace InfceCalbckEvCtrl

{

    public partial class UserControl1 : UserControl

    {

        //声明接口类型

        public interface ICallbackEvn

        {

            void ShowObjTxt(string Txt);

        }

        //定义(但不实例化)接口对象

        public ICallbackEvn ObjCallEvn;

        public UserControl1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            ObjCallEvn.ShowObjTxt("接口回调仿真事件调用成功!");

        }

    }

}

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.