home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!uunet.ca!frumious!pat
- From: pat@frumious.uucp (Patrick Smith)
- Subject: Re: C++ Variable Argument Lists (,...)
- Message-ID: <1992Sep10.002546.561@frumious.uucp>
- Date: Thu, 10 Sep 1992 00:25:46 GMT
- Reply-To: uunet.ca!frumious!pat
- References: <37835@sdcc12.ucsd.edu>
- Organization: None
- Lines: 41
-
- acanter@sdcc3.ucsd.edu (Adriaan Canter) writes:
- |Is there any way that a function with a variable argument list
- |(as indicated by ellipses) can tell how many variable arguments have
- |been passed to it?
-
- There is no portable way (at least, none that I know of).
-
- But if you're willing to accept a maximum number of arguments,
- then there are a couple of solutions.
-
-
- First, if there is a value that can't be legally used as an
- argument, then you can do something like this:
-
- void foo(int arg1 = 0, int arg2 = 0, int arg3 = 0, int arg4 = 0);
-
- and have foo() ignore any arguments that are set to 0 (or whatever
- illegal value you use).
-
-
- Second, you can have the main function call another function that
- does the actual work:
-
- void bar(unsigned number_of_arguments, ...);
-
- inline void foo()
- { bar(0); }
- inline void foo(int arg1)
- { bar(1, arg1); }
- inline void foo(int arg1, int arg2)
- { bar(2, arg1, arg2); }
- // etc.
-
- Actually, you don't need bar(); you could just have several overloaded
- versions of foo(), with each one doing all the necessary work.
- But this duplication of code could quickly become tiresome.
-
- --
- Patrick Smith
- uunet.ca!frumious!pat
- pat%frumious.uucp@uunet.ca
-