home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19790 < prev    next >
Encoding:
Text File  |  1993-01-23  |  2.9 KB  |  94 lines

  1. Path: sparky!uunet!cs.utexas.edu!tamsun.tamu.edu!tamsun.tamu.edu!news
  2. From: ski8032@tamsun.tamu.edu (Suman Kumar Inala)
  3. Newsgroups: comp.lang.c++
  4. Subject: Help w/ default arguments and more
  5. Date: 22 Jan 1993 18:41:09 -0600
  6. Organization: Texas A&M University
  7. Lines: 82
  8. Message-ID: <1jq475INNp69@tamsun.tamu.edu>
  9. NNTP-Posting-Host: tamsun.tamu.edu
  10. Keywords: default arguments swapping []
  11.  
  12. I would like to be able to do something like this:
  13.  
  14. class X {
  15.     int x;
  16. public:
  17.     X(void) { x = 3; }
  18.     int func(int arg = this->x) { return arg; }
  19. };
  20.  
  21. In other words, I would like the default argument to
  22. one of a classes methods to be one of the members of
  23. the class.  This is of course possible if I were
  24. to make x static, but this is not acceptable.  BC complains
  25. that this may only be used in member functions; simply
  26. expressing "int arg = x" gives the legitimate complaint
  27. that x is not associated with an object.  The
  28. following is a workaround, but I don't like it much:
  29.  
  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.  
  38. Is there a better solution?  Also, I know I'm missing
  39. something simple here, but the following doesn't work:
  40.  
  41.  
  42. #define SWAP_INT(a, b) { a ^= b; b ^= a; a ^= b; }
  43. inline void swap(int a, int b) {  a ^= b; b ^= a; a ^= b; }
  44.  
  45. The SWAP_INT macro works, but not my inline function.  Why?
  46.  
  47. Also, I declared a class Map which contains a 2D array.  I
  48. would have liked to overload the [] operator to take two
  49. arguments, so that I could treat it in a natural way,
  50. indexing the elements of the map using array notation.
  51. (Is this perhaps bad practise?)
  52.  
  53. Map map(80, 20); // Dimensions of map are 80 columns, 20 rows
  54.  
  55. cout << map[3][4];
  56.  
  57. But the [] operator may only be declared to take one argument.
  58. It would be nice if the following notation could be used were
  59. it possible for [] to take more than one argument, though it
  60. is not traditional C notation:
  61.  
  62. cout << map[3, 4];
  63.  
  64. So my question is why [] is limited to one argument.   I don't
  65. think that doing so would not break any existing code, since
  66. if the [] operator was declared to have only one operand, the
  67. expression
  68.  
  69. cout << map[3, 4]  <==> cout << map[4]
  70.  
  71. but otherwise would know to pass 3 & 4 to the operator []
  72. function, similar to the way the ()'s operator works.
  73.  
  74. Finally, I derived the class xstream from iostream to provide
  75. a streamable screen class for mode X (a graphics mode on the
  76. PC).  The question is, when should I use a parameterized
  77. manipulator and when should I use a simple function call.
  78. Right now, I have a parameterized manipulator which takes 4
  79. arguments:
  80.  
  81. xout << setviewport( 20, 20, 200, 210 ) << "text... "; // etc
  82.  
  83. Is it a good practise to have parameterized manipulators
  84. with more than two arguments?  Does it matter?  Should
  85. I say xout.setviewport( ... ) instead?
  86.  
  87. Thank you for your time.
  88.  
  89. -- 
  90. Till next time,                \o/   \o/
  91.                                 V \o/ V     email:pinky@tamu.edu
  92. <>  Sam  Inala  <>                 V
  93.  
  94.