home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Wisdom on f(...,const class& xxx = default_xxx)
- Message-ID: <1992Nov16.174152.16576@taumet.com>
- Organization: TauMetric Corporation
- References: <mg.721758488@tyrolia>
- Date: Mon, 16 Nov 1992 17:41:52 GMT
- Lines: 49
-
- mg@tyrolia (Michael Golan) writes:
-
-
- >what's the net wisdom on a default parameter which is a class? ...
-
- >My best solution is
-
- >T Default_T(0) ; // global default class T
-
- >and
-
- >void f(...., const T& = Default_T);
-
- >But this goes "against" OO, and pollute the name space. Anyone has a better
- >solution (trick?)
-
- I suggest not using default parameters, but using overloaded functions
- instead. This avoids quite a few problems associated with default
- parameters. Example:
- void f(int, const T&);
-
- void f(int i)
- {
- static T t;
- f(i, t);
- }
-
- If you can't stand the overhead of an extra function call, you could
- combine this with someone else's suggestion of a static class member
- (because an inline function can't have static local variables).
- Example:
-
- class T {
- public:
- static T t; // for default parameters of type T
- ...
- };
- void f(int, const T&);
- inline void f(int i) { f(i, T::t); }
-
- When default parameters are not simple literals, they sometimes result
- in unexpected behavior due to scope interactions, and sometimes
- interact unexpectedly with function overloading. Using explicit
- overloading instead as above often results in more reliable and
- maintainable programs.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-