home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!charon.amdahl.com!netcomsv!netcomsv!ulogic!hartman
- From: hartman@ulogic.UUCP (Richard M. Hartman)
- Newsgroups: comp.lang.c++
- Subject: Re: References to functions?
- Message-ID: <930@ulogic.UUCP>
- Date: 27 Jan 93 19:34:48 GMT
- References: <1k2u7nINNgbc@info2.rus.uni-stuttgart.de> <1993Jan26.162624.14112@fcom.cc.utah.edu>
- Organization: negligable
- Lines: 68
-
- In article <1993Jan26.162624.14112@fcom.cc.utah.edu> swillden@cs.weber.edu writes:
- |KOCHER@nvdv.e-technik.uni-stuttgart.dbp.de (HARTMUT KOCHER) writes:
- |: In <1993Jan25.182507.13761@fcom.cc.utah.edu> swillden@news.ccutah.edu writes:
- |: > : Not exactly. Let's say I have a callback class which takes a pointer
- |: > : to a function in its constructor. If I use a pointer, a 0-pointer may be
- |: > : passed. If I use a reference instead, I can make sure, that the user
- |: > : supplies a valid function.
- |: > Not really. Try:
- |: > typedef int (callbackfunc)(int);
- |: > extern void set_callback(callbackfunc&);
- |: > void foo()
- |: > {
- |: > callbackfunc* callback = 0;
- |: > set_callback(*callback);
- |: > }
- |: > : Hartmut Kocher
- |: > Shawn Willden
- |: > swillden@icarus.weber.edu
- |:
- |: Sure, but it's always undefined to dereference a 0 pointer.
- |
- |Yes, it is undefined, not illegal. A compiler could warn about it in
- |this case, I suppose, but a quick experiment shows that g++ 2.3.3, and
- |Cfront 3.0.1 accept the above code without complaint and there are
- |many situations in which the compiler couldn't know.
-
- The fact remains that you have to pull a convoluted trick to
- get this result. If you do things like that you deserve to
- have your machine seize up when the reference gets used...
-
- |: With a reference you always clearly state, that you are
- |: expecting a valid object/function, etc.
- |
- |This is true, as long as you realize that you are only stating that
- |you expect a valid object/function. You are not forcing the the user
- |to supply a valid argument, which was what I understood your intent to
- |be.
-
- And with a pointer it is frequently allowable (expected even) that
- a NULL pointer is allowable under certain circumstances. With a
- reference you are making it clear that you should provide something
- valid.
-
- |: Therefore, it's the callers responsibility to make sure he's passing a valid
- |: argument, whereas with pointers, the function implementor has to check for
- |: 0 pointers, because these are allowed by the language.
- |
- |It's always the caller's responsibility to pass a valid argument. I
- |agree that passing a reference *should* make it clear to the user that
- |he needs to pass a valid function. If you intended the reference
-
- Sometimes NULL *is* a valid argument. A reference argumetn indicates
- that it is not. A pointer argument leaves the point unclear.
-
- |parameter to be a reminder then I agree. This is, however, a
- |strictly stylistic thing, not an example of a need for function
- |references. Now, if we had a way to impose run-time checks on the
- |validity of pointers that are dereferenced and cast to references...
-
- I think the overall benefit makes this more than a stylistic thing.
-
-
- -Richard Hartman
- hartman@ulogic.COM
-
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- I am just a figment of your imagination...
- ...what does that say about *your* mental health?
-