Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The function pointer usage of C++

Shulou Source: shulou.com Published: 2022-06-02 07:08:05 09月25日 Update

This article mainly explains "the function pointer usage of C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn the function pointer usage of C++.

When looking at the C++ code, I saw a very novel usage. I came back to analyze it. I feel that although the principle is very simple, it is worth recording.

The code looks something like this:

Class test {

Public:

Void A1 () {printf ("test::a1\ n");}

}

Int main ()

{

Test * t = new test ()

Void (test::*a) (void)

A = & test::a1

(t-> * a) ()

}

In the past, when using function pointers in C++, we always used static member functions reflectively, but I never thought that ordinary member functions could also use function pointers.

After careful analysis, the principle has been known for a long time. It's all about this pointers.

The only difference between using the pointer of the test object to call a function and the ordinary C function pointer is that the pointer of the test object will be passed as the this pointer through the ecx register, so all C++ class member functions essentially access their member variables through the value of ecx.

In order to verify the above conclusion more clearly without disassembly, the following code is added:

Void (* b) (void)

B = (void (*) (void)) & test::a1

B ()

Finally, you can find that the function can still run.

Of course, this is actually a bit of a trick, because the test::a1 function does not access the member variables of the class, so we can call this member function directly as an ordinary function.

If the test::a1 function accesses the member variables, the test::a1 function will crash due to a random ECX value because the C function call does not pass a this pointer.

But this code is enough to prove that & test::a1 gives the absolute address of a function no different from that of an ordinary C function, so the difference between calling b () and (t-> * a) () lies in whether a this pointer is passed.

If you have a little more hack spirit, you can actually change the test::a1 function to access member variables, and then before calling the C function, you can manually embed asm code to force the ECX value to the pointer value of t. If the effect is the same, then you can fully prove the above conclusion.

Thank you for your reading, the above is the content of "the function pointer usage of C++". After the study of this article, I believe you have a deeper understanding of the function pointer usage of C++. The specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Functions pointers members Clippers code ordinary variables that is learning content principles addresses objects situations conclusions verification novelty unexpected in use registers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker Redmi vpn Xiaomi