Topic: Pointer to functions (Was: atexit() behavior)
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/12/01 Raw View
Hans Olsson wrote:
> * What is specified about math-functions (in <cmath>).
> In CD2 it is implementation-defined if they are extern "C" or extern "C++"
> and constructing a table of pointers to them is thus not possible.
Implementors are encouraged to make them C++ functions,
but that's _not_ required.
As ever, directly taking a pointer to a C library function
is unportable:
char* (*p) (char*, const char*) = &std::strcpy; // unportable
You can wrap the fucntion:
char* my_strcpy (char*, const char*);
char* (*p) (char*, const char*) = my_strcpy; // ok
Note that by wraping the function you can also use other
arguments or return types:
void my_strcpy (char*, const char*);
void (*p) (char*, const char*) = my_strcpy; // ok
Or you can use overloading:
extern "C"
typedef char* (*c_func) (char*, const char*);
extern "C++" // extern directive is redondant here
typedef char* (*cpp_func) (char*, const char*);
void use_func (c_func);
void use_func (cpp_func);
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]