home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!yale.edu!ira.uka.de!news.belwue.de!news.uni-stuttgart.de!news.uni-stuttgart.de!news
- From: KOCHER@nvdv.e-technik.uni-stuttgart.dbp.de (HARTMUT KOCHER)
- Newsgroups: comp.lang.c++
- Subject: Re: References to functions?
- Date: 26 Jan 1993 08:54:15 GMT
- Organization: University of Stuttgart, IND (Germany)
- Lines: 46
- Distribution: world
- Message-ID: <1k2u7nINNgbc@info2.rus.uni-stuttgart.de>
- References: <1k0d3dINNev9@info2.rus.uni-stuttgart.de> <1993Jan25.182507.13761@fcom.cc.utah.edu>
- NNTP-Posting-Host: nvdv01.ind.e-technik.uni-stuttgart.de
- X-News-Reader: VMS NEWS 1.24
- In-Reply-To: swillden@news.ccutah.edu's message of Mon, 25 Jan 93 18:25:07 GMT
-
- In <1993Jan25.182507.13761@fcom.cc.utah.edu> swillden@news.ccutah.edu writes:
- > : > References to functions are legal, but not very useful. I can't
- > : > think of anything you can do with a reference to a function you
- > : > can't do with a const pointer to a function in exactly the same way,
- > : > except for trivial syntax differences at the point of initialization.
- > : > Steve Clamage, TauMetric Corp, steve@taumet.com
- > : 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.
- With a reference you always clearly state, that you are
- expecting a valid object/function, etc.
-
- 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.
-
- Hartmut Kocher
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-