home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 20080 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  3.1 KB

  1. Path: sparky!uunet!crdgw1!rpi!usc!howland.reston.ans.net!spool.mu.edu!agate!doc.ic.ac.uk!pipex!demon!trmphrst.demon.co.uk!nikki
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help w/ default arguments and more
  5. Message-ID: <728159529snx@trmphrst.demon.co.uk>
  6. Date: 27 Jan 93 11:32:09 GMT
  7. References: <1jq475INNp69@tamsun.tamu.edu>
  8. Sender: usenet@demon.co.uk
  9. Reply-To: nikki@trmphrst.demon.co.uk
  10. Organization: Trumphurst Ltd.
  11. Lines: 76
  12. X-Mailer: cppnews $Revision: 1.31 $
  13.  
  14. In article <1jq475INNp69@tamsun.tamu.edu> ski8032@tamsun.tamu.edu (Suman Kumar Inala) writes:
  15. > I would like to be able to do something like this:
  16. > class X {
  17. >     int x;
  18. > public:
  19. >     X(void) { x = 3; }
  20. >     int func(int arg = this->x) { return arg; }
  21. > };
  22. > In other words, I would like the default argument to
  23. > one of a classes methods to be one of the members of
  24. > the class.  This is of course possible if I were
  25. > to make x static, but this is not acceptable.  BC complains
  26. > that this may only be used in member functions; simply
  27. > expressing "int arg = x" gives the legitimate complaint
  28. > that x is not associated with an object.  The
  29. > following is a workaround, but I don't like it much:
  30. > class X {
  31. >     int x;
  32. > public:
  33. >     X(void) { x = 3; }
  34. >     int func(void) { return x; }
  35. >     int func(int arg) { return arg; }
  36. > };
  37. > Is there a better solution?  
  38.  
  39. I don't think there is. If arg never takes a particular value (e.g. -1), 
  40. then you can do ...
  41.       int func(int arg = -1) { return (arg == -1) ? x : arg; }
  42.  
  43. This is often most useful for pointer arguments, which you default to 
  44. NULL.
  45.  
  46. > Also, I know I'm missing
  47. > something simple here, but the following doesn't work:
  48. > #define SWAP_INT(a, b) { a ^= b; b ^= a; a ^= b; }
  49. > inline void swap(int a, int b) {  a ^= b; b ^= a; a ^= b; }
  50. > The SWAP_INT macro works, but not my inline function.  Why?
  51.  
  52. Because you are passing the arguments by value. An inline function has
  53. exactly the same effect as a non-inline function (indeed, the compiler may
  54. arbitrarily decide not to inline it). You need ...
  55.  
  56.   inline void swap(int &a, int &b) {  a ^= b; b ^= a; a ^= b; }
  57.  
  58. > Also, I declared a class Map which contains a 2D array.  I
  59. > would have liked to overload the [] operator to take two
  60. > arguments, so that I could treat it in a natural way,
  61. > indexing the elements of the map using array notation.
  62. > (Is this perhaps bad practise?)
  63. > Map map(80, 20); // Dimensions of map are 80 columns, 20 rows
  64. > cout << map[3][4];
  65. > But the [] operator may only be declared to take one argument.
  66. > It would be nice if the following notation could be used were
  67. > it possible for [] to take more than one argument, though it
  68. > is not traditional C notation:
  69. > cout << map[3, 4];
  70.  
  71. Surely it would be better to be able to use map[3][4], just like a "real" 
  72. array. There are ways of doing this - usually using a helper class which 
  73. emulates a single dimension of the array. I believe there is an example 
  74. in the FAQ for this group.
  75.  
  76. -- 
  77. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  78. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  79.