Functions Flashcards

1
Q

How do you declare a function pointer?

A
<return type> (*<ptr_fun>)(args type...)

for example:

int (p_fun)(const char)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you decalre a pointer function as a porperty in a class?

A
class A
{
    void (*ptr_f)(const char*);
public:
    A(void (*func)(const char*)):ptr_f{func}{}

    void raise_func(const char* s)
    {
        if(ptr_f) ptr_f(s);
    }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a **Functor*?

A

It’s an Object that is used as a function which overrides the operato () to invoke several functions like:
~~~
class A{
public:
int operator()(int x, int y){ return x+y;}
void operator()(const char* name){ cout &laquo_space;“Hello “ &laquo_space;name; }
};
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the syntaxis to declare a lambda?

A
auto <lmb_name> = [<captures>](<args>) mutable noexpect -><return tuype>
{
<Body>
return <value>;
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In lambda function, what is the function of mutable keyword?

A

It allows to modify the variables that were capture by copy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a functor?

A

Son objetos que se utilizan como funciones, reescribiendo el operador operator()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you write a functor which returns a bool value from a comparation of two integers?

A
class Comparator
{
public:

bool operator()(int a, intb){return a==b;}

}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does std::bind do and what is its header file?

A

From <functional>

It create a wrapper arround a function and, the wraper, can be used in several context to call the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Having the next function:

int fx(int y, string s)

How do you create a wrapper, using std::bind.

A
#include<functional>

using namespace std;
using namespace std::placeholders;

int fx(int x, string s)
{   
    cout << s << "\n";
    return x*10;
}
int main()
{   
    auto f = std::bind(fx, _1, _2);
    cout << "result " << f(23, "Hello");
    return 0;
}

Output:
~~~
result Hello
230
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will be the output of this code and why?
~~~
int fx(int x, int y, string id, int z)
{
cout &laquo_space;”[” &laquo_space;id &laquo_space;”] “;
cout «“x: “ &laquo_space;x &laquo_space;”; y: “ &laquo_space;y &laquo_space;”; z: “ &laquo_space;z &laquo_space;“\n”;
return x*10;
}
int main()
{
auto f = std::bind(fx, _2, _4, “MyId”, _1);
cout &laquo_space;“result “ &laquo_space;f(1, 23, “MyOther”, 55);
return 0;
}
~~~

A
result [MyId] x: 23; y: 55; z: 1
230

Because you are declaring the bind as std::bind(fx, x, y, s, z) but at the same time you are mapping the parameter with the placeholdersas:
~~~
f( x, y, s, z)
f( 1, 23, “MyOther”, 55)
bind(fx, _2, _4, “MyId”, _1)
~~~
which means that:
+ x, for fx, will be taken from the second parameter (_2) of bind (23).
+ y, for fx, will be taken from the fourth parameter (_4) of bind (55).
+ z, for fx, will be taken from the second parameter (_1) of bind (1).
+ s always will be “MyId” because it was declared as a constant on the bind.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What kind of object is retuned by std::bind?

A

std::function which is the wrapper of the function that must declare the protptype of the function

Function: int fx(int x, int y, string id, int z)
Declaration: std::function<int(int,int,string,int)>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly