typedef

H

hyderabadblues

I have problem.I want to create function pointer with typedef so that
I can use it for both functions which takes one integer parameter and
function which take none because of some obvious reasons of code
merging from two different projects.

Something like

type void (*FP)(int i=0);

But above piece of code gives me a error, that I cannot have default
parameters.How can I change my code
 
G

Gavin Deane

I have problem.I want to create function pointer with typedef so that
I can use it for both functions which takes one integer parameter and
function which take none because of some obvious reasons of code
merging from two different projects.

Something like

type void (*FP)(int i=0);

But above piece of code gives me a error, that I cannot have default
parameters.How can I change my code

The presence or otherwise of default values for parameters does not
affect the type of a function.

void f(int i);
void g(int j = 0);
void h();

f and g are of type void(*)(int) whereas h is of type void(*)(). Those
types are different and so you simply can not have one typedef that
covers both. I'm not too clear on your requirements. If you need to
call a function that takes an int, but you are happy to always use the
default value for the parameter, you could write a simple wrapper:

void g_wrapper()
{
g(); // Calls g with the default value for the parameter j (zero
in this example)
}

Now g_wrapper and h have the same type: void(*)() so you can do this

typedef void (*fp)();
fp g_ptr = g_wrapper;
fp h_ptr = h;

but you can't use g_wrapper to call g with any value other than 0.

Alternatively, you could do it the other way round - provide a wrapper
for h that takes a dummy parameter

void h_wrapper(int) { h(); }

Now f, g and h_wrapper all have type void (*)(int)

typedef void (*fp2)(int);

fp2 f_ptr = f;
fp2 g_ptr = g;
fp2 h_ptr = h_wrapper;

f_ptr(42); // Call f with i == 42
g_ptr(42); // Call g with j == 42
h_ptr(0); // Call h via h_wrapper (the 0 is ignored)

But this all gets a bit cumbersome if you have a lot of functions to
worry about, because each equivalent to h will need its own wrapper.
With a better description of your problem, a better solution might be
apparent. If the above doesn't help, post some code:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Gavin Deane
 
P

paul.joseph.davis

The presence or otherwise of default values for parameters does not
affect the type of a function.

void f(int i);
void g(int j = 0);
void h();

f and g are of type void(*)(int) whereas h is of type void(*)(). Those
types are different and so you simply can not have one typedef that
covers both. I'm not too clear on your requirements. If you need to
call a function that takes an int, but you are happy to always use the
default value for the parameter, you could write a simple wrapper:

void g_wrapper()
{
g(); // Calls g with the default value for the parameter j (zero
in this example)

}

Now g_wrapper and h have the same type: void(*)() so you can do this

typedef void (*fp)();
fp g_ptr = g_wrapper;
fp h_ptr = h;

but you can't use g_wrapper to call g with any value other than 0.

Alternatively, you could do it the other way round - provide a wrapper
for h that takes a dummy parameter

void h_wrapper(int) { h(); }

Now f, g and h_wrapper all have type void (*)(int)

typedef void (*fp2)(int);

fp2 f_ptr = f;
fp2 g_ptr = g;
fp2 h_ptr = h_wrapper;

f_ptr(42); // Call f with i == 42
g_ptr(42); // Call g with j == 42
h_ptr(0); // Call h via h_wrapper (the 0 is ignored)

But this all gets a bit cumbersome if you have a lot of functions to
worry about, because each equivalent to h will need its own wrapper.
With a better description of your problem, a better solution might be
apparent. If the above doesn't help, post some code:http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Gavin Deane

Another method would be to use functors and bind parameters.

boost::function and boost::bind are an example of this:
http://www.boost.org/doc/html/function.html
http://www.boost.org/libs/bind/bind.html

HTH,
Paul Davis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top