function pointers and Function objects

Source: Internet
Author: User

function pointers (global functions/class member functions), function objects (functions object)

One. The function pointer type is a global function.

#include "stdafx.h"

#include <iostream>
Usingnamespace Std;

Class Testaction;

typedefvoid (*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;
}

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

#include "stdafx.h"

#include <iostream>
Usingnamespace Std;

Class Action;
Class Testaction;

function pointer type is a member function of class Action
typedefvoid (Action::* FP) (int);

Class Action
{
Public
void Drink (int i)
{
cout<<"No." <<i<<"Drink ..." <<endl;
}

void Eat (int i)
{
cout<<"No." <<i<<"Eat ..." <<endl;
}
};

Class Testaction
{
Public
Define a function pointer
FP testact;
Action object instance, which is used to record the action object being instantiated
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::D rink;
Doact. Testact (0);
Doact. Testact (1);
Doact. Testact (2);
Doact.testact = Action::eat;
Doact. Testact (0);
Doact. Testact (1);
Doact. Testact (2);
return 0;
}

Three. Functional objects (Function object)
This paper turns from C++builder study-Http://www.ccrun.com/article.asp?i=1005&d=sc37og

#include "stdafx.h"

#include <iostream>
#include <functional>

Usingnamespace Std;

Class Action;
Class Drink;
Class Eat;
Class Testaction;

Class Action
{
Public
IntOperator () (int i)
{
ACT (i);
return i;
}

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

Although transfer function pointers are widely used in event-driven systems, the implementation of callback functions is invoked through pointers. But C + + provides another alternative, the function object, which avoids the use of function pointers. There are several advantages to doing so. First, because the object can be modified internally without altering the external interface, the design is more flexible and more resilient. The function object also has a data member that stores the result of the previous call. In addition, the compiler can inline function objects to further enhance performance. Function objects can express a general algorithm that relies on member templates, which is difficult to accomplish with ordinary function pointers. The example uses a function object to implement a general negation algorithm operation:

#include "stdafx.h"
#include <iostream>

Usingnamespace Std;

Class Negate
{
Public
template<Class T> TOperator () (T-t) const
{
Return-t;
}
};

void Callback (int N,Const negate& NEG)Passing a Function object
{
n = Neg (n);Call the overloaded () operation to perform a negate operation on n
cout << n << Endl;
}

int main (int argc, char* Argv[])
{
//call way One
Callback (5, Negate ());

//call mode two
Negate neg;
cout << neg (9.99999) << Endl;
cout << neg (__int32 (39999999)) << Endl;

return 0;
} The

STL Library defines a number of function objects for the relevant algorithm calls, such as templated function objects greater<> or less<>:

Vector < int> VI;
//: Fill vector
Sort (vi.begin (), Vi.end (), Greater<int> ()); span class= "comment" >//descending (Descending)
Sort (Vi.begin (), Vi.end (), Less<int> ());  //Ascending (Ascending)

http://blog.csdn.net/adcxf/article/details/3969717

function pointers and Function objects

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.