home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13450 < prev    next >
Encoding:
Text File  |  1992-09-09  |  1.5 KB  |  53 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!uunet.ca!frumious!pat
  3. From: pat@frumious.uucp (Patrick Smith)
  4. Subject: Re: C++ Variable Argument Lists (,...)
  5. Message-ID: <1992Sep10.002546.561@frumious.uucp>
  6. Date: Thu, 10 Sep 1992 00:25:46 GMT
  7. Reply-To: uunet.ca!frumious!pat
  8. References: <37835@sdcc12.ucsd.edu>
  9. Organization: None
  10. Lines: 41
  11.  
  12. acanter@sdcc3.ucsd.edu (Adriaan Canter) writes:
  13. |Is there any way that a function with a variable argument list
  14. |(as indicated by ellipses) can tell how many variable arguments have
  15. |been passed to it?
  16.  
  17. There is no portable way (at least, none that I know of).
  18.  
  19. But if you're willing to accept a maximum number of arguments,
  20. then there are a couple of solutions.
  21.  
  22.  
  23. First, if there is a value that can't be legally used as an
  24. argument, then you can do something like this:
  25.  
  26.    void foo(int arg1 = 0, int arg2 = 0, int arg3 = 0, int arg4 = 0);
  27.  
  28. and have foo() ignore any arguments that are set to 0 (or whatever
  29. illegal value you use).
  30.  
  31.  
  32. Second, you can have the main function call another function that
  33. does the actual work:
  34.  
  35.    void bar(unsigned number_of_arguments, ...);
  36.  
  37.    inline void foo()
  38.       { bar(0); }
  39.    inline void foo(int arg1)
  40.       { bar(1, arg1); }
  41.    inline void foo(int arg1, int arg2)
  42.       { bar(2, arg1, arg2); }
  43.    // etc.
  44.  
  45. Actually, you don't need bar(); you could just have several overloaded
  46. versions of foo(), with each one doing all the necessary work.
  47. But this duplication of code could quickly become tiresome.
  48.  
  49. -- 
  50. Patrick Smith
  51. uunet.ca!frumious!pat
  52. pat%frumious.uucp@uunet.ca
  53.