home *** CD-ROM | disk | FTP | other *** search
- 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
- From: nikki@trmphrst.demon.co.uk (Nikki Locke)
- Newsgroups: comp.lang.c++
- Subject: Re: Help w/ default arguments and more
- Message-ID: <728159529snx@trmphrst.demon.co.uk>
- Date: 27 Jan 93 11:32:09 GMT
- References: <1jq475INNp69@tamsun.tamu.edu>
- Sender: usenet@demon.co.uk
- Reply-To: nikki@trmphrst.demon.co.uk
- Organization: Trumphurst Ltd.
- Lines: 76
- X-Mailer: cppnews $Revision: 1.31 $
-
- In article <1jq475INNp69@tamsun.tamu.edu> ski8032@tamsun.tamu.edu (Suman Kumar Inala) writes:
- > 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?
-
- I don't think there is. If arg never takes a particular value (e.g. -1),
- then you can do ...
- int func(int arg = -1) { return (arg == -1) ? x : arg; }
-
- This is often most useful for pointer arguments, which you default to
- NULL.
-
- > 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?
-
- Because you are passing the arguments by value. An inline function has
- exactly the same effect as a non-inline function (indeed, the compiler may
- arbitrarily decide not to inline it). You need ...
-
- inline void swap(int &a, int &b) { a ^= b; b ^= a; a ^= b; }
-
- > 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];
-
- Surely it would be better to be able to use map[3][4], just like a "real"
- array. There are ways of doing this - usually using a helper class which
- emulates a single dimension of the array. I believe there is an example
- in the FAQ for this group.
-
- --
- Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
- trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
-