typedef
s
<cctype>
predicates to strings.
bool isdigitstr(const std::string & str); bool isspacestr(const std::string & str);
and so on.
bool isdigitstr(const std::string & str) for (unsigned i = 0; i < str.size(); i++) if !std::isdigit(str[i]) return false return true bool isspacestr(const std::string & str) for (unsigned i = 0; i < str.size(); i++) if !std::isspace(str[i]) return false return true
and so on.
open()
, close()
, read()
, write()
, ...
return-type-spec ( * fun-name ) ( argument-list-decls )
bool (* comp)(int x, int y)
declares a
comp
that
int
parameters and
bool
value.
bool (* comp)(int, int)
typedef
stypedef bool (* comp_fun)(int x, int y)
typedef
name.
void sort(int a[], comp_fun less) { ... }
comp_fun orders[] = { less, equal, greater }
bool (* comp)(int, int)
declares comp
as a pointer to a
function accepting two int
s and returning a bool
.
bool * comp(int, int)
declares comp
as a function
accepting two int
s and return a pointer to a bool
.
int i; void (* f)(int x); std::cout << i; // whatever (* f)(3); // disaster
bool less(int x, int y) { . . . } bool (* comp)(int x, int y) = &less;
void less1(int x, int y) { . . . } bool less2(int x) { . . . } bool less3(int x, float y) { . . . }bool (* comp1)(int x, int y) = &less1;bool (* comp2)(int x, int y) = &less2;bool (* comp3)(int x, int y) = &less3;
*
goes to the function to execute.
bool b = (* cmpr)(3, 4);
fp(...)
to (* fp)(...)
.
bool b = cmpr(3, 4);
*
must appear in function pointer declarations.
&
is not necessary.
bool (* comp)(int x, int y) = less;
()
::
is highest).
(* comp)(1, 2)
is not the same as
* comp(1, 2)
, identical to * (comp(1, 2))
.
()
can be overloaded.
bool isstrkind(string str, int (* cp)(int)) for (unsigned i = 0; i < str.size(); i++) if !cp(str[i]) return false return true isstrkind(str, isspace) isstrkind(str, isdigit)
_if
ones, in particular).
widget::widget(args a, void (* err_fun)(args)) if terrible error err_fun(a)
struct device_driver { int (* open)(const char * name, mode_t mode); int (* close)(void); int (* read)(char * data, unsigned size); int (* write)(const char * data, unsigned size); }
device_driver disk_device_driver = { disk_open, disk_close, disk_read, disk_write }; device_driver tty_device_driver = { tty_open, tty_close, tty_read, tty_write }; void t(void) { device_driver dd = disk_device_driver; (dd.open)("/dev/null", R_ONLY); }
.*
and ->*
syntax (neither overloadable).
operator ()
is much more fun, though.
This page last modified on 4 March 2003.