Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use imitating function in C++

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how C++ uses imitation functions, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

The so-called imitation function is a call method very similar to function call. In fact, imitation function only overloads the () operator.

This approach is very common in STL container functions, which are divided into function objects and predicates

class t

{

public:

void operator ()(stu & a) function object (unary)

/*

bool operator ()(stu & a) predicate (unary), predicates only return boolean values

*/

};

void test (stu & a) function

So call it, and we can clearly see

imitated function call

t lfun;

lfun(a);

where lfun is the class object defined

Function calls are

test(a);

Their calls look remarkably similar.

The following demonstrates how to use the imitation function

Click here to fold or open

/** ***********************************************************************

> File Name: 201000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

> Author: gaopeng QQ:22389860 all right reserved

> Mail: gaopp_200217@163.com

> Created Time: Sun 23 Apr 2017 08:03:41 PM CST

************************************************************************/

#include

#include

#include

#include

using namespace std;

class testfun//imitation function

{

public:

testfun(void)

{

cnt = 0;

}

void operator()(int& a)

{

cnt++;

if( ! (a%67))

{

cout

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report