regarding function pointers

S

sam_cit

Hi Everyone,

I just saw in a site, http://www.newty.de/fpt/intro.html, that
function pointers can be used for all functions having same singnature
(parameter type(sS) and return type), and i tried the following code
and it worked fine although the return type was different for the two
functions,

int func1()
{
printf("in func1...\n");
return(1);
}

char func2()
{
printf("in func2...\n");
return('c');
}

int main()
{
int (*p)();
p = &func1;
p();
p = &func2;
p();
}

i tried in Microsoft VC++ 6.0, is it an undefined behavior? Please let
me know...
 
M

mlimber

I just saw in a site, http://www.newty.de/fpt/intro.html, that
function pointers can be used for all functions having same singnature
(parameter type(sS) and return type), and i tried the following code
and it worked fine although the return type was different for the two
functions,

int func1()
{
printf("in func1...\n");
return(1);
}

char func2()
{
printf("in func2...\n");
return('c');
}

int main()
{
int (*p)();
p = &func1;
p();
p = &func2;
p();
}

i tried in Microsoft VC++ 6.0, is it an undefined behavior? Please let
me know...

It doesn't compile for me with VC6 (<OT>do you have the service pack 6
installed?</OT>). Try it on the compilers found here:

http://dinkumware.com/exam/Default.aspx

Cheers! --M
 
N

Noah Roberts

Hi Everyone,

I just saw in a site, http://www.newty.de/fpt/intro.html, that
function pointers can be used for all functions having same singnature
(parameter type(sS) and return type), and i tried the following code
and it worked fine although the return type was different for the two
functions,

int func1()
{
printf("in func1...\n");
return(1);
}

char func2()
{
printf("in func2...\n");
return('c');
}

int main()
{
int (*p)();
p = &func1;
p();
p = &func2;
p();
}

i tried in Microsoft VC++ 6.0, is it an undefined behavior? Please let
me know...

It shouldn't compile. The types don't match. Your compiler is
non-std...this is well known for that version. I would get a different
compiler for sure.

If you want that kind of behavior look at boost::function. It
implements an interface that will be in the next standard.
 
S

Salt_Peter

Hi Everyone,

I just saw in a site, http://www.newty.de/fpt/intro.html, that
function pointers can be used for all functions having same singnature
(parameter type(sS) and return type), and i tried the following code
and it worked fine although the return type was different for the two
functions,

Its not so much the signature, its the fact that the compiler is
silently converting an integer to a char.
Which is not surprising from VC6.
int func1()
{
printf("in func1...\n");
return(1);
}

char func2()
{
printf("in func2...\n");
return('c');
}

int main()
{
int (*p)();
p = &func1;
p();
p = &func2;
p();
}

i tried in Microsoft VC++ 6.0, is it an undefined behavior? Please let
me know...

Your compiler should respect the type-checking involved.
Have you considered a template?
You still need to define the appropriate function pointer.

#include <iostream>
#include <ostream>

template< typename T >
void func(T& t)
{
++t;
}

int main()
{
void (*p_fn)(int&);

int n(5);
p_fn = &func;
p_fn(n);
std::cout << "n = " << n << std::endl;

void (*p_fc)(char&);

char c('a');
p_fc = &func;
p_fc(c);
std::cout << "c = " << c << std::endl;

}

/*
n = 6
c = b
*/
 
H

Howard

Salt_Peter said:
Its not so much the signature, its the fact that the compiler is
silently converting an integer to a char.
Which is not surprising from VC6.

Where would it be doing that?

Even in VC6, I have to cast the second one like this in ordetr to compile:
p = (int(*)())&func2;

But when I do, it calls func2, not func1. And func2 returns a char. So,
what integer is getting converted to a char, and where?

To the OP: It doesn't compile for me in VC6, unless I cast it, at least not
with the default compiler settings. Are you using the default settings?

-Howard
 
S

sam_cit

To the OP: It doesn't compile for me in VC6, unless I cast it, at least not
with the default compiler settings. Are you using the default settings?

-Howard

Actually, i tried with function returning float value too rather than
char just to avoid implicit type conversion by the compiler, if any,
however, i see a warning returned by the compiler, it happens to be,

warning C4133: '=' : incompatible types - from 'float (__cdecl *)()' to
'int (__cdecl *)()'

I guess that answers the question...
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top