home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!tamsun.tamu.edu!tamsun.tamu.edu!news
- From: ski8032@tamsun.tamu.edu (Suman Kumar Inala)
- Newsgroups: comp.lang.c++
- Subject: Help w/ default arguments and more
- Date: 22 Jan 1993 18:41:09 -0600
- Organization: Texas A&M University
- Lines: 82
- Message-ID: <1jq475INNp69@tamsun.tamu.edu>
- NNTP-Posting-Host: tamsun.tamu.edu
- Keywords: default arguments swapping []
-
- I would like to be able to do something like this:
-
- class X {
- int x;
- public:
- X(void) { x = 3; }
- int func(int arg = this->x) { return arg; }
- };
-
- In other words, I would like the default argument to
- one of a classes methods to be one of the members of
- the class. This is of course possible if I were
- to make x static, but this is not acceptable. BC complains
- that this may only be used in member functions; simply
- expressing "int arg = x" gives the legitimate complaint
- that x is not associated with an object. The
- following is a workaround, but I don't like it much:
-
- class X {
- int x;
- public:
- X(void) { x = 3; }
- int func(void) { return x; }
- int func(int arg) { return arg; }
- };
-
- Is there a better solution? Also, I know I'm missing
- something simple here, but the following doesn't work:
-
-
- #define SWAP_INT(a, b) { a ^= b; b ^= a; a ^= b; }
- inline void swap(int a, int b) { a ^= b; b ^= a; a ^= b; }
-
- The SWAP_INT macro works, but not my inline function. Why?
-
- Also, I declared a class Map which contains a 2D array. I
- would have liked to overload the [] operator to take two
- arguments, so that I could treat it in a natural way,
- indexing the elements of the map using array notation.
- (Is this perhaps bad practise?)
-
- Map map(80, 20); // Dimensions of map are 80 columns, 20 rows
-
- cout << map[3][4];
-
- But the [] operator may only be declared to take one argument.
- It would be nice if the following notation could be used were
- it possible for [] to take more than one argument, though it
- is not traditional C notation:
-
- cout << map[3, 4];
-
- So my question is why [] is limited to one argument. I don't
- think that doing so would not break any existing code, since
- if the [] operator was declared to have only one operand, the
- expression
-
- cout << map[3, 4] <==> cout << map[4]
-
- but otherwise would know to pass 3 & 4 to the operator []
- function, similar to the way the ()'s operator works.
-
- Finally, I derived the class xstream from iostream to provide
- a streamable screen class for mode X (a graphics mode on the
- PC). The question is, when should I use a parameterized
- manipulator and when should I use a simple function call.
- Right now, I have a parameterized manipulator which takes 4
- arguments:
-
- xout << setviewport( 20, 20, 200, 210 ) << "text... "; // etc
-
- Is it a good practise to have parameterized manipulators
- with more than two arguments? Does it matter? Should
- I say xout.setviewport( ... ) instead?
-
- Thank you for your time.
-
- --
- Till next time, \o/ \o/
- V \o/ V email:pinky@tamu.edu
- <> Sam Inala <> V
-
-