home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16369 < prev    next >
Encoding:
Text File  |  1992-11-16  |  1.6 KB  |  60 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Wisdom on f(...,const class& xxx = default_xxx)
  5. Message-ID: <1992Nov16.174152.16576@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <mg.721758488@tyrolia>
  8. Date: Mon, 16 Nov 1992 17:41:52 GMT
  9. Lines: 49
  10.  
  11. mg@tyrolia (Michael Golan) writes:
  12.  
  13.  
  14. >what's the net wisdom on a default parameter which is a class? ...
  15.  
  16. >My best solution is 
  17.  
  18. >T Default_T(0) ;    // global default class T
  19.  
  20. >and
  21.  
  22. >void f(...., const T& = Default_T);
  23.  
  24. >But this goes "against"  OO, and pollute the name space. Anyone has a better
  25. >solution (trick?)
  26.  
  27. I suggest not using default parameters, but using overloaded functions
  28. instead.  This avoids quite a few problems associated with default
  29. parameters.  Example:
  30.     void f(int, const T&);
  31.  
  32.     void f(int i)
  33.     {
  34.         static T t;
  35.         f(i, t);
  36.     }
  37.  
  38. If you can't stand the overhead of an extra function call, you could
  39. combine this with someone else's suggestion of a static class member
  40. (because an inline function can't have static local variables).
  41. Example:
  42.  
  43.     class T {
  44.     public:
  45.         static T t;    // for default parameters of type T
  46.         ...
  47.     };
  48.     void f(int, const T&);
  49.     inline void f(int i) { f(i, T::t); }
  50.  
  51. When default parameters are not simple literals, they sometimes result
  52. in unexpected behavior due to scope interactions, and sometimes
  53. interact unexpectedly with function overloading.  Using explicit
  54. overloading instead as above often results in more reliable and
  55. maintainable programs.
  56. -- 
  57.  
  58. Steve Clamage, TauMetric Corp, steve@taumet.com
  59. Vice Chair, ANSI C++ Committee, X3J16
  60.