当前位置:网站首页>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);
}边栏推荐
- Is your database "cloud native"?
- JD Logistics: from giant baby to mainstay
- What is the performance improvement after upgrading the 4800h to the 5800h?
- [51nod] 3395 n-bit gray code
- Industry ranks first in blackmail attacks, hacker organizations attack Afghanistan and India | global network security hotspot
- What is the meaning of scdo? Is it comparable to bGH
- I have a server. What can I do?
- Introduce the comparison of various distributed configuration centers? Which distributed configuration center is better?
- Coding helps Jiangsu high-speed information to achieve organizational agility and R & D agility, leading the new infrastructure of Intelligent Transportation
- AI talk | solution for shopping mall inspection robot
猜你喜欢

2022-2028 global medical modified polypropylene 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

2022-2028 global tungsten copper alloy industry research and trend analysis report

2022-2028 global cell-based seafood industry research and trend analysis report
![[51nod] 3395 n-bit gray code](/img/b5/2c072a11601de82cb92ade94672ecd.jpg)
[51nod] 3395 n-bit gray code
![[51nod] 3216 Awards](/img/94/fdb32434d1343040d711c76568b281.jpg)
[51nod] 3216 Awards

Permission maintenance topic: domain controller permission maintenance

2022-2028 global anti counterfeiting label industry research and trend analysis report

2022-2028 global marine clutch industry research and trend analysis report
![[summary of interview questions] zj5](/img/d8/ece82f8b2479adb948ba706f6f5039.jpg)
[summary of interview questions] zj5
随机推荐
Afnetworking usage and cache processing
How does easydss handle the problem that the sharing page cannot be opened due to cache problems?
[51nod] 2653 section XOR
Mutual conversion between qstring and qdatetime
golang with script
Disk and file system (Simplified)
Why enterprises need fortress machines
Is the cloud desktop server highly required for installation and configuration? Is cloud desktop easy to use?
New Google brain research: how does reinforcement learning learn to observe with sound?
AI talk | solution for shopping mall inspection robot
2022-2028 global third-party data platform industry research and trend analysis report
Introduce the comparison of various distributed configuration centers? Which distributed configuration center is better?
Gin framework: RPC error code design
2022-2028 global aircraft audio control panel system industry research and trend analysis report
MySQL case deep excavation information_ Root causes of slow schema view query (Part 2)
C common regular expression collation
What is cloud desktop and how to connect to the server? What does the mainstream architecture of cloud desktop include?
South Korea's national network is disconnected. Who launched the network "attack"?
Innovation or hype? Is low code a real artifact or a fake tuyere?
Is the IP of the fortress machine the IP of the server? How to deploy the fortress machine