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;
}