* Alf P. Steinbach:
* Chris M. Thomasson:
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:gusnlv$v4s$1@news.eternal-september.org...
* questioner_x@yahoo.com:
I have a few questions about C++.
1. Of what use is a reference to a function? Why not just use a
pointer, which can be checked against NULL?
Why would you /want/ to have to check against 0?
[...]
I guess sometimes it can be useful.
Yah.
But...
"sometimes useful" != "good idea as the only mechanism to use"
Think of the following contrived example; quick pseudo-code:
______________________________________________________________
typedef void (*fp_handler) (struct foo*);
struct foo {
fp_handler custom_handler;
/* [...] */
};
fp_handler
foo_swap_custom_handler(
struct foo* const self,
fp_handler custom_handler
) {
fp_handler old = self->custom_handler;
self->custom_handler = custom_handler;
return old;
}
void
foo_do_something(
struct foo* const self
) {
/* [...] */
if (self->custom_handler) {
self->custom_handler(self);
} else {
foo_sys_default_handler(self);
}
/* [...] */
}
It seems you didn't realize you were posting to [comp.lang.c++], but
thought you were posting to a C group?
Would go like this in C++:
class Foo;
typedef void (*FpHandler)( Foo& );
class Foo
{
private:
FpHandler myFpHandler;
public:
Foo(): myFpHandler( &sysDefaultHandler ) {}
FpHandler setHandler( FpHandler fph )
{
if( !fph ) { fph = &sysDefaultHandler; }
std::swap( fph, myFpHandler );
return fph;
}
void doSomething() { myFpHandler(); }
};
Uhm, add the missing argument. :-)
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking