Get the App
SLTechnology News&Howtos  ›  Development  › 

The structure of member function pointers and how to convert them to ordinary function pointers

Shulou Source: shulou.com Published: 2022-06-03 05:52:29 09月29日 Update

This article introduces the structure of the member function pointer and how to convert it with the ordinary function pointer. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Arbitrary pointers can be implemented through memory copy (memcpy, etc.)

But there is no guarantee that it can be used properly.

Through online search, it is found that:

The function member pointer is actually different from the ordinary member pointer, it contains other information besides the address of the function itself (such as whether it is a virtual function, etc.), so we can not simply understand that the member function pointer is a normal pointer, which generally occupies 4 bytes, depending on the compiler: for example, in VS, the ordinary member function pointer is similar to

Struct ptr {

Int * addr

}

The structure of the virtual function is more complex, it is through the this pointer and index to obtain the real address of the function, is not fully understood, this will not repeat.

Here is the method to obtain the real address of the member function:

1. Ordinary member function

Through observation, it is not difficult to find that the first address of the structure is the first address of the addr, so the entry address of the member function is actually the address of the function pointer, but C++ does not allow them to convert to other ordinary pointers for type safety reasons, such as:

Class test {public: void print () {}}; typedef void (test::*cfun) (); typedef void (* fun) (); cfun cf = & test::print;fun f = cf; / / failed, type check memcpy (& fmaijingcfmaisizeof (fun)); f (); / / success

two。 Virtual function

(1) obtain it through virtual function table.

Class test {public: virtual void print () {}}; typedef void (test::*cfun) (); typedef void (* fun) (); test t * vptr = (int**) (& t); / / vptr [0] gets the virtual function table address cfun f = vptr [0] [0]; / / the index of the virtual function in the virtual function table at 00:00 represents the first virtual function f () ((fun) vptr [0] [0]) ()

It is usually not possible to use & test::print to get a virtual function address, even if the address is an intermediate value or always returns 0x1.

3. General pointer conversion function

TemplateR convert (t) {long addr = 0; memcpy (& addr,&t,sizeof (long)); return (R) (addr);}

However, the effectiveness of the conversion can not be guaranteed.

About the structure of the member function pointer and how to share the conversion between the ordinary function pointer and here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Function pointer address member normal structure different between content that is more type guarantee help good complex effective secure successful that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft MariaDB Shulou Technology Xiaomi