当前位置:网站首页>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);
}边栏推荐
- Where is the cloud game server? Can individuals rent cloud game servers?
- Mutual conversion between qstring and qdatetime
- golang with script
- Permission maintenance topic: domain controller permission maintenance
- PHP verify mailbox format
- [summary of interview questions] zj5
- South Korea's national network is disconnected. Who launched the network "attack"?
- How much does it cost to rent a cloud game server? Which cloud game server is more reliable?
- Implementing an ORM framework against SQL injection with builder mode
- Disaster recovery series (V) -- database disaster recovery construction
猜你喜欢

UI automation based on Selenium

Permission maintenance topic: domain controller permission maintenance
![[51nod] 3216 Awards](/img/94/fdb32434d1343040d711c76568b281.jpg)
[51nod] 3216 Awards

2022-2028 global portable two-way radio equipment industry research and trend analysis report

2022-2028 global cancer biopsy instrument and kit industry research and trend analysis report

2022-2028 global pilot night vision goggle industry research and trend analysis report
![[51nod] 2102 or minus and](/img/68/0d966b0322ac1517dd2800234d386d.jpg)
[51nod] 2102 or minus and

2022-2028 global marine clutch industry research and trend analysis report

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

2022-2028 global genome editing mutation detection kit industry survey and trend analysis report
随机推荐
Concise and practical time code
Hook principle
Elk7.15.1 installation, deployment and construction
Storage crash MySQL database recovery case
Prompt error when Jekyll runs cannot load such file -- webrick (LoadError)
What is the role of the distributed configuration center? What are the advantages of a distributed configuration center?
Is the IP of the fortress machine the IP of the server? How to deploy the fortress machine
golang with script
Uipickerview show and hide animation
Grc: GRC interface is mixed with restful API
How to access the server through the fortress machine? What's the use of the fortress machine?
LeetCode 205. Isomorphic Strings
How to install the cloud desktop security server certificate? What can cloud desktops do?
Why do cloud desktops use rack servers? Why choose cloud desktop?
Crawler series: using API
2022-2028 global portable two-way radio equipment industry research and trend analysis report
Coding helps Jiangsu high-speed information to achieve organizational agility and R & D agility, leading the new infrastructure of Intelligent Transportation
Afnetworking server client
Where is the cloud game server? Can individuals rent cloud game servers?
14. Tencent cloud IOT device side learning - data template application development