当前位置:网站首页>RI Geng series: tricks of using function pointers
RI Geng series: tricks of using function pointers
2022-06-24 03:02:00 【mariolu】
One 、 What is a function pointer
A function pointer It's a kind of C、C++、 Other categories C The pointer to language .
C Language standards , Function indicator (function designator, That is, the function name ) Neither lvalue , Nor is it an R-value . but C++ Language standards specify that function indicators belong to lvalues , Therefore, the conversion of the function indicator to the right value of the function pointer belongs to the conversion of the left value to the right value .
Two 、 Function pointer application
Function pointers are used to abstract interfaces . Mask function implementation process .
The most classic example is the size of two objects .
typedef (bool)(*CompareFuncT) (struct Object, struct Object);
struct Object&
select(struct Object A, struct Object B, CompareFuncT compare) {
if (compare(A, B)) return A;
else return B;
}Here you can pass in various comparison functions , Just follow CompareFuncT Interface functions .
This interface function can be a normal function , Static function of class , Can member functions of a class ?
3、 ... and 、 Can I use a member function of a class as a function pointer
Let's take another example :
We have 1 An interface class , And implementation classes that implement interface classes .
so what , We operate on him manipulate, This operation will traverse all the implementation classes , And use this to call the interface implemented by each class . Look at the pseudo code
class TrancatedCalculator {
virtual bool IsTrancated(std::shared_ptr<Context>&, Item&){};
}
class FilterActiveApp : public TrancatedCalculator {
bool IsTrancated(std::shared_ptr<ScheduleRankContext>&, Item&) override;
}
void do() {
std::vector<std::unique_ptr<TrancatedCalculator>> truncated_calculator_vec;
for (auto& calculator: truncated_calculator_vec) {
truncated_calculator_vec->IsTruncated(item);
}
}Okay , Here we continue to expand do The logic of , Then I think we need to follow the single blame principle of function . And then in do There is another one inside truncate function , And then put truncated_calculator_vec->IsTrancated Pass on the past , Whether this is feasible , Can you write like this ?
void foo(IsTruncatedFuncT fn, Item item) {return;}
void do(){
std::vector<std::unique_ptr<TrancatedCalculator>> truncated_calculator_vec;
for(auto& calculator: truncated_calculator_vec){
//truncated_calculator_vec->IsTruncated(item);
foo(truncated_calculator_vec->IsTruncated, item);// Can you write it like this ? The answer is no
}
}Obviously it can't be written like this ,
One problem that needs to be explained here is “ Member pointer ” Instead of ordinary function pointers .
A member pointer to a function is more than just a function pointer . On the implementation side , The compiler cannot use simple function addresses , Because you don't know the address to call ( Think about virtual functions ). Of course , You also need to know the object to provide this Implicit parameter .
If you want to provide a function pointer to existing code , You should write static member functions of your classes . Static member functions do not require this, So you need to pass this object as an explicit parameter .
But what if you have to pass in a member function , Because sometimes member functions change some member variables of a class , Can't be static .
We can design it like this :
bool
has_truncated(std::unique_ptr<TrancatedCalculator>& tc, std::shared_ptr<Context>& ctx, Item& item) {
return tc->IsTrancated(ctx, item);
}Then we pass in except has_truncated Outside the function pointer , You need to bring in std::unique_ptr<TrancatedCalculator>& object . And then here we are foo Can design
void foo(IsTruncatedFuncT fn, std::shared_ptr<Context>& ctx,
std::unique_ptr<TrancatedCalculator>& tc, Item& item) {
fn(tc, ctx, item);
}边栏推荐
- Visual AI, first!
- Grc: GRC interface is mixed with restful API
- Mutual conversion between qstring and qdatetime
- 2022-2028 global aircraft wireless intercom system industry research and trend analysis report
- Is your posture correct—— A detailed discussion on horizontal sub database and sub table
- System design: File Hosting Service
- Ner's past, present and future Overview - past
- [summary of interview questions] zj6 redis
- The 2021 Tencent digital ecology conference is coming
- Lua language development, esp8266 access to Bafa cloud, mqtt and TCP protocols
猜你喜欢
![[51nod] 3216 Awards](/img/94/fdb32434d1343040d711c76568b281.jpg)
[51nod] 3216 Awards

UI automation based on Selenium
![[51nod] 2106 an odd number times](/img/af/59b441420aa4f12fd50f5062a83fae.jpg)
[51nod] 2106 an odd number times

2022-2028 global aircraft front wheel steering system industry research and trend analysis report

2022-2028 global indoor pressure monitor and environmental monitor industry research and trend analysis report

The cost of on-site development of software talent outsourcing is higher than that of software project outsourcing. Why

Permission maintenance topic: domain controller permission maintenance

2022-2028 global aircraft audio control panel system industry research and trend analysis report

IOS development - multithreading - thread safety (3)

2022-2028 global portable two-way radio equipment industry research and trend analysis report
随机推荐
What are the configuration requirements for cloud desktop servers? What are the main characteristics of the three points?
MySQL Cases-MySQL 8.0.26 bug ERROR 1064 (42000) at line1: You have an error
Grand summary of boutique idea plug-ins! Worth collecting
Innovation or hype? Is low code a real artifact or a fake tuyere?
How much does it cost to rent a cloud game server? Which cloud game server is more reliable?
Visual AI, first!
How to understand EDI requirements of trading partners
[summary of interview questions] zj5
PHP verify mailbox format
How to build your own cloud game server and what are the building steps
2022-2028 global genome editing mutation detection kit industry survey and trend analysis report
Create and mount large files
What are the benefits of rack servers for cloud desktops? What can cloud desktop do?
Gartner released the magic quadrant of enterprise low code platform in 2021. Low code integrated platform becomes a trend!
Liaoyuan social cloud primary actual combat camp test environment script
Mutual conversion between qstring and qdatetime
[1024 programmers' day] Why do some programmers leave work earlier than you?
How to transfer files from the server connected to the fortress machine and how to access the server through the fortress machine
Tencent Youtu won the champion of iccv2021 LVIs challenge workshop and the best innovation award of the project
What is the case when easynvr non administrator logs in to view the empty video list?