How can I dynamically Save the addresses of member functions of different classes of objects? -Solution

Source: Internet
Author: User

Question:

To implement a container class, you can save different types of function addresses, including member functions, and
Objects belong to different classes, that is, objects are different, and functions are different (parameters are the same as return values );
If you do not know how to save the function addresses of different classes, note: the life cycle of the saved address is
Same as the object's life cycle;

Study process:

1. One night of study, we found that the information of different functions can be saved (that is, the function address can be used as a char array
Can be saved by using the memcpy function,), but the function type (that is, class) needs to be specified during the call.
(Or explanation) the address of the saved function, but the type is unknown, so it can be saved but read (used) is not
The key is that the type cannot be saved or used again. I don't know how to save the type.
.
(Alas, the pointer to C ++ is too inflexible ).

2. After studying this day, I found that the solution to this problem is "delegation". The following article describes

Very detailed:
Original article: Member Function Pointers and the Fastest Possible C ++ Delegates
Http://www.codeproject.com/cpp/FastDelegate.asp
Member function pointers and high-performance C ++ Delegation
Http://blog.csdn.net/hifrog/archive/2004/07/03/33068.aspx
Source code: http://bbs.nju.edu.cn/file/Y/yangjiudan/delegate.rar

The author writes a class to solve the C ++ delegation problem.
I understand it like this,
There should be no problem with common and static member functions, just use the regular method;
For non-static member functions, the call needs to know the object address T * O, the object type T, the member function's
Address ReturnType (T: * menfunc) (InputType );
The object type can be obtained through the function template T, then the object address O, the member function address memfunc will know
,
You can call this function through (O-> * menfunc) (InputType para;
This allows you to call member functions of different types of objects.
However, calling a function is immediate, that is, you can only call the function in the function that obtains the function information.
Number of called functions, so you cannot use a function to call the function.
Add them, and then call them in another function. Therefore, this cannot be achieved.

Therefore, the key issue is to save different types of objects and their member functions each time, that is, one
Variables of the corresponding type are stored, but the types are unknown and different, and only a limited number of types can be known through the class template.
(In this case, the user can pass in the type to be used through the template parameter, but the number of types is
And declare a variable for each input type within the class to store the added call function information.
In the Add function, use RTTI to store the corresponding information to the corresponding variable.
You can call this function successfully, but the implementation method is very stupid (you can't do it ),
Therefore, we can only ask him to send another message. This is the method of the author: this is probably the case. The key lies in the storage of object information,
Although the information of different types of objects is different, the addresses and member function locations of different types of objects in the same compiler are
The address structure (composition) is the same (but the content of different types will be different), so you can define a general
Type (the maximum address structure), so that other types of address information can be forcibly converted (reinter_cast
) And no information will be lost. This step is implemented through union, so that the required information is guaranteed.
Read (use) is required below. As mentioned above, the call of member functions also requires specific types of information.
But the original type is still not saved (this is not possible), only the converted type information, how can this problem be solved?
If something is useless, isn't it the same? No, that's because you won't use it. How can you use it? Like the Compiler
For the same purpose, the compiler uses the object address and the structure of the member function address based on the type to finally obtain the real location.
Address (see the original article), so we only need to know the type like the compiler (that is, when adding a function)
Save the real address.
Then, when calling, read the real address and call the phase through (* object address-> * member function real address) (input parameter)
The corresponding function is actually the thing that should have been done by the compiler, so you can do what the compiler can't do.
One thing is that the call to a function is implemented through a function object (that is, the overload () operator), So you
The running result of the function, not the function.
Oh, it's really messy. I don't know what I understand, right? I hope I can take a good look at the author's original text and source code,
Let's have a good chat.
There is a demo in the source code. You can check the effect.

Summary:

Is the implementation of the Delegate in C ++. The function class in boost can be used (not verified ),

The Fastde Delegate mentioned above can also be implemented (with high efficiency). In addition, there is a class of articles in CodeProject about delegation;

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.