home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!gatech!darwin.sura.net!spool.mu.edu!agate!dog.ee.lbl.gov!hellgate.utah.edu!fcom.cc.utah.edu!swillden
- From: swillden@news.ccutah.edu (Shawn Willden)
- Subject: Re: References to functions?
- Message-ID: <1993Jan26.162624.14112@fcom.cc.utah.edu>
- Sender: news@fcom.cc.utah.edu
- Reply-To: swillden@cs.weber.edu
- Organization: University of Utah Computer Center
- X-Newsreader: Tin 1.1 PL3
- References: <1k2u7nINNgbc@info2.rus.uni-stuttgart.de>
- Date: Tue, 26 Jan 93 16:26:24 GMT
- Lines: 50
-
- 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.
-
- : 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.
-
- : 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
- 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...
-
- : Hartmut Kocher
-
- --
- Shawn Willden
- swillden@icarus.weber.edu
-