jk@cs.man.ac.uk (John Kewley ICL) (05/24/91)
The following program is supposed to print 81, the square of 9.
Unfortunately I would prefer to use a function which takes a constant
int pointer as a parameter to pass to g rather than a variable one.
This program fails to compile because of the const on the line labelled below.
//---------------------------------------------------------
#include <stdio.h>
typedef int int_fn( int* );
int g(const int_fn* fn, int i)
{
return(fn(&i));
}
int f(const int* i) // CC complains here
{
return( *i**i );
}
int main()
{
printf( "i=%d\n",g(f,9) );
return(0);
}
//---------------------------------------------------------
Error message:
line 17: error: bad argument 1 type for g(): int (*)(const int *)
( int (*)(int *) expected)
//---------------------------------------------------------
Why is this, apart from "because the syntax is defined like that", why is
a function with consts on its parameters not valid in place of one with
variable as parameters?
--
J.K.
John M. Kewley, ICL, Wenlock Way, West Gorton, Manchester. M12 5DR
Tel: (+44) 61 223 1301 X2138 Email: jk@cs.man.ac.uk / jk@nw.stl.stc.co.ukdarcy@druid.uucp (D'Arcy J.M. Cain) (05/26/91)
In article <2569@m1.cs.man.ac.uk> jk@cs.man.ac.uk (John Kewley ICL) writes: >The following program is supposed to print 81, the square of 9. >Unfortunately I would prefer to use a function which takes a constant >int pointer as a parameter to pass to g rather than a variable one. Then do it. >typedef int int_fn( int* ); >int g(const int_fn* fn, int i) >int f(const int* i) // CC complains here This line is actually OK. you have to change the earlier part: typedef int int_fn(const int* ); // if you want the function to take a pointer to const then say so int g(int_fn* fn, const int i) // the typedef already tells C++ that the function takes a pointer // to a const int >line 17: error: bad argument 1 type for g(): int (*)(const int *) Note that the error suggests the proper typedef. -- D'Arcy J.M. Cain (darcy@druid) | D'Arcy Cain Consulting | There's no government Toronto, Ontario, Canada | like no government! +1 416 424 2871 |
pete@borland.com (Pete Becker) (05/29/91)
In article <2569@m1.cs.man.ac.uk> jk@cs.man.ac.uk (John Kewley ICL) writes: >Why is this, apart from "because the syntax is defined like that", why is >a function with consts on its parameters not valid in place of one with >variable as parameters? A function that takes const parameters promises not to modify those parameters. A function that takes non-const parameters makes no such promise. It is legal to pass either a const or a non-const object to a function that takes a const parameter, because the constness of the object is preserved. It is not legal to pass a const object to a function that takes a non-const parameter, because the function could modify the passed parameter. In this situation, the compiler will create a temporary and pass that as the actual parameter, so the original object will not, in fact, be modified. But when you use a pointer to a function, there's no way for the compiler to know, at the point of the function call, whether to generate that temporary. So the rule on function pointers has to be more strict: the parameter types must agree completely.
Pete.Becker@sunbrk.FidoNet.Org (Pete Becker) (05/29/91)
In article <2569@m1.cs.man.ac.uk> jk@cs.man.ac.uk (John Kewley ICL) writes: >Why is this, apart from "because the syntax is defined like that", why is >a function with consts on its parameters not valid in place of one with >variable as parameters? A function that takes const parameters promises not to modify those parameters. A function that takes non-const parameters makes no such promise. It is legal to pass either a const or a non-const object to a function that takes a const parameter, because the constness of the object is preserved. It is not legal to pass a const object to a function that takes a non-const parameter, because the function could modify the passed parameter. In this situation, the compiler will create a temporary and pass that as the actual parameter, so the original object will not, in fact, be modified. But when you use a pointer to a function, there's no way for the compiler to know, at the point of the function call, whether to generate that temporary. So the rule on function pointers has to be more strict: the parameter types must agree completely. * Origin: Seaeast - Fidonet<->Usenet Gateway - sunbrk (1:343/15.0)