A summary of function Pointers and function objects in C++

  • 2020-04-02 01:14:05
  • OfStack

Function pointer
Function pointer:
Is a pointer to a function, at C compile time, each function has an entry address, so the function pointer to this function points to that address.

Function Pointers are useful for two main purposes: to call functions and to do function parameters.

Declaration method of function pointer:
Datatype identifier (pointer variable name) (parameter list);
The declaration of the general function is:
Int func of int x;
The declaration method of a function pointer is:
Int (* func) (int x);
The (*func) parenthesis is necessary to tell the compiler that we are declaring a function pointer instead of declaring a function with a return type as a pointer, depending on which function parameter the function pointer points to.
However, this declaration can sometimes be cumbersome, so a typedef can come in handy, or we can declare:
Typedef int (*PF) (int x);
PF PF.
Pf is thus a function pointer, much more convenient. Func (x) or   *fucn (x) will do, of course, function Pointers can also point to overloaded functions, and the compiler will distinguish between the overloaded functions so that the function pointer points to the correct function.
Example:


typedef void (*PFT) ( char ,int );
void bar(char ch, int i)
{
    cout<<"bar "<<ch<<' '<<i<<endl;
    return ;
}
PFT pft;
pft = bar;
pft('e',91);

In the example, the function pointer PFT points to a declared function bar(), and then USES PFT to output characters and integers.
Function pointer to another function is as a function of parameters, we can in a function's parameter list to a function pointer, you can then use this function in the function pointer to a function, it can make the program more clear and concise, but such use techniques that can help us solve many difficult problems, using a small price to pay to get enough interests (speed) + complexity.

typedef void (*PFT) ( char ,int );
void bar(char ch, int i)
{
    cout<<"bar "<<ch<<' '<<i<<endl;
    return ;
}
void foo(char ch, int i, PFT pf)
{
    pf(ch,i);
    return ;
}
PFT pft;
pft = bar;
foo('e',12,pft);

In the above example, we first use a function pointer PFT to point to bar(), and then use the PFT pointer to call bar() in the foo() function to achieve the purpose. By taking advantage of this feature, we can build powerful programs that can call different bar functions with the same foo function.

Function object
Front is the application of function pointer, from the general function callback sense, function object and a function pointer is the same, but it has many function object function pointer does not have a bit, function object programming is more flexible, and can realize the function of the inline (inline) calls, make the whole process implementation performance acceleration.

Function object: We've shown here that this is an object, and it's actually only the function function that this object has that we call a function object, which makes sense. If an object has the function function, we call it a function object.
How to make an object functional is as simple as overloading the operator () of the object, as follows:


class A{
public:
int operator()(int x){return x;}
};
A a;
a(5);

So a becomes a function object, and when we execute a(5), we actually take advantage of the overloading symbol ().
Since the function object is a "class object", then of course we can call it in the function parameter list, it can completely replace the function pointer! If the pointer is the symbol of C and the class is unique to C++, then we can also say that the relationship between pointer functions and function objects is the same as the former! (though a bit tight). When we want to call a function in the parameter list, we can first declare a function object with such function function, and then use this object in the parameter. The function object does the same function as the function pointer, and it is more secure.
Here's an example:

class Func{
public:
    int operator() (int a, int b)
    {
        cout<<a<<'+'<<b<<'='<<a+b<<endl;
        return a;
    }
};
int addFunc(int a, int b, Func& func)
{
    func(a,b);
    return a;
}
Func func;
addFunc(1,3,func);

The above example first defines a function object class and overloads the () operator to add and output the first two arguments, then USES the class object in the parameter list in addFunc to add the two Numbers.
If using generic thinking to consider, you can determine a function template class, to achieve the addition of general types of data:

class FuncT{
public:
    template<typename T>
    T operator() (T t1, T t2)
    {
        cout<<t1<<'+'<<t2<<'='<<t1+t2<<endl;
        return t1;
    }
};
template <typename T>
T addFuncT(T t1, T t2, FuncT& funct)
{
    funct(t1,t2);
    return t1;
}
FuncT funct;
addFuncT(2,4,funct);
addFuncT(1.4,2.3,funct);

The well-known STL will be widely used in this technology, the details can be seen in the master of some generic technology books, do not think that the frequent invocation of function objects will make the program performance greatly discounted, a lot of facts and experiments prove that the correct use of the function object program is much faster than the performance of other programs! Therefore, to master and skillfully use the function object to our program, otherwise...
In this way, function objects open up a window for C++, but with them come some complex problems and pitfalls that we still need to learn and explore.


Related articles: