In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.