Function as a parameter of another function

Hello,

I apologize for my poor programming vocabulary, so I will try to explain it simple.

There are functions that take into its parameters another function, like the atexit() as below:
1
2
3
void blah() { ... }

atexit( blah );


However, it seems not to work when the other function has parameters too, as below:
1
2
3
void blah(int b) { ... }

atexit( blah(2) ); // error: "void" is incompatible with parameter of type "void (__cdecl *)() 

When using functions that are, in the parameter input aspect, similar to atexit() it always results in incompatibility issues.

How can I specify the function's parameters inside the parameter of another function?

I would really appreciate some help on a solution or workaround for this.

Thank you.
Last edited on
First you should see the reference: http://www.cplusplus.com/reference/clibrary/cstdlib/atexit/
The function has to return no value and accept no arguments.
Then you have to understand, that your're passing a pointer of a function. That is a place where the function starts. It contains no additional data, such as its arguments. If you want to pass arguments too, read this thread : http://www.cplusplus.com/forum/general/27541/
Last edited on
When you pass a function as a parameter to another function you only pass the address of the function. You can not specify the parameters because passing the address of the function is not the same as calling the function. It is down to the function that you passed it to to supply the parameters when it calls the passed in function.
A workaround would be using global variables, or even better a static class interface:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

class Blah
{
private:
    ~Blah();
    Blah();

public:
    static int param1;
    static char param2;
    //etc...

    static void blah()
    {
        cout << param1 << endl;
        cout << param2 << endl;
        cout << "hit enter to quit...";
        cin.get();
    }
};

int Blah::param1;
char Blah::param2;

int main()
{
    atexit(Blah::blah);

    Blah::param1=2;
    Blah::param2='c';

    return 0;
}

AFAIK, it is guaranteed that the registered function (Blah::blah) will be called before the destruction of any static objects created before the registration, so this should not cause any problems.

EDIT: Mmm... I'm not sure for cout... Maybe someone else knows better...
Last edited on
closed account (1yR4jE8b)
How feasible would it be to change this atexit function to be a template? You could then pass it function objects which contain the paramters as member variables like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

template <class T> void atexit(const T& functor);

class Functor
{
public:
	typedef int param_type1;
	typedef int param_type2;

	Functor(param_type2 param1, param_type2 param2) :
		param_one(param1), param_two(param2)
	{
	}
	
	void operator()() const
	{
		std::cout << "First parameter plus second parameter is: " << (param_one + param_two) << "\n";
	}
	
private:
	param_type1 param_one;
	param_type2 param_two;
};

int main()
{
	atexit(Functor(23, 56)); //for example
	
    return 0;
}

template <class T>
void atexit(const T& functor)
{
	functor();
}

closed account (1yR4jE8b)
oh wow, I never realized atexit was the standard C Library function...derp a derp....that's how much I do C >.<
You can not specify the parameters because passing the address of the function is not the same as calling the function.
That's what I was afraid of hearing.

I really appreciate your time and your codes. I took a look at them a while ago but I happened to forget to reply :)

Thank you.
But you didn't read the next line:

It is down to the function that you passed it to to supply the parameters when it calls the passed in function.

Since you have the code and you can modify it, of course you can do it.

You can wrap the caller in a class and have it call with the proper parameters (don't really have to be static, either), exactly like the way darkestfright coded it up.
Last edited on
kfmfe04 wrote:
don't really have to be static, either

If you want to wrap them in a class and use the C library atexit, they have to be static. The function has to be static (since that's what atexit expects), therefore the params have to be static (because a static function can't operate on non-static members).
Topic archived. No new replies allowed.