Function pointers and function objects

Source: Internet
Author: User

Function object: in some way, it is like a function object. Typically, it refers to an instance of a class, which defines the application Operator ().

Function pointer: It is widely used in event-driven systems to call callback functions through pointers.

Advantages of function objects: 1. Because objects can be modified internally without modifying external interfaces, the design is more flexible and flexible. 2. Have data members that store the previous call results. 3. the compiler can inline function objects to further enhance performance. 4. General algorithms that can specifically express dependency member templates are difficult to accomplish with common function pointers.

Example:

1. The function pointer type is a global function.

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Class testaction;

Typedef void (* FP) (INT );

Void drink (int I)
{
Cout <"no." <I <"drink..." <Endl;
}

Void eat (int I)
{
Cout <"no." <I <"eat..." <Endl;
}

Class testaction
{
Public:
FP testact;

Void testact (int I)
{
If (testact! = NULL)
{
Testact (I );
}
}
};

Int main (INT argc, char * argv [])
{
Testaction doact;
Doact. testact = & drink;
Doact. testact (0 );
Doact. testact (1 );
Doact. testact (2 );
Doact. testact = & eat;
Doact. testact (0 );
Doact. testact (1 );
Doact. testact (2 );
Return 0;
}

2. The function pointer type is a class member function.

# Include "stdafx. H"
# Include <iostream>
Using namespace STD;

Class action;
Class testaction;

// The function pointer type is an action-like member function.
Typedef void (Action: * FP) (INT );

Class action
{
Public:
Void drink (int I)
{
Cout <"no." <I <"drink..." <Endl;
}

Void eat (int I)
{
// This article from C ++ builder research-http://www.ccrun.com/article.asp? I = 1005 & D = sc37og
Cout <"no." <I <"eat..." <Endl;
}
};

Class testaction
{
Public:
// Define a function pointer
FP testact;
// Action object instance. the pointer is used to record the instantiated action object.
Action * paction;

Void testact (int I)
{
If (paction! = NULL) & (testact! = NULL ))
{
// Call
(Paction-> * testact) (I );
}
}
};

Int main (INT argc, char * argv [])
{
Action Act;
Testaction doact;
Doact. paction = & Act;
Doact. testact = Action: drink;
Doact. testact (0 );
Doact. testact (1 );
Doact. testact (2 );
Doact. testact = Action: Eat;
Doact. testact (0 );
Doact. testact (1 );
Doact. testact (2 );
Return 0;
}

3. function object)

# Include "stdafx. H"
# Include <iostream>
# Include <functional>

Using namespace STD;

Class action;
Class drink;
Class eat;
Class testaction;

Class action
{
Public:
Int operator () (int I)
{
Act (I );
Return I;
}

Virtual void act (int I) = 0;
};

Class drink: public action
{
Void Act (int I)
{
Cout <"no." <I <"drink..." <Endl;
}
};

Class eat: public action
{
Void Act (int I)
{
Cout <"no." <I <"eat..." <Endl;
}
};

Class testaction
{
Public:
Void testact (int I, Action & testact)
{
Testact (I );
}
};

Int main (INT argc, char * argv [])
{
Testaction doact;
Doact. testact (0, drink ());
Doact. testact (1, drink ());
Doact. testact (2, drink ());
Doact. testact (0, eat ());
Doact. testact (1, eat ());
Doact. testact (2, eat ());
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.