home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- From: nikki@trmphrst.demon.co.uk (Nikki Locke)
- Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
- Subject: Re: smart enums
- Reply-To: nikki@trmphrst.demon.co.uk
- References: <1993Jan21.054036.27343@hparc0.aus.hp.com>
- Distribution: world
- X-Mailer: cppnews $Revision: 1.31 $
- Organization: Trumphurst Ltd.
- Lines: 44
- Date: Fri, 22 Jan 1993 15:53:06 +0000
- Message-ID: <727743186snx@trmphrst.demon.co.uk>
- Sender: usenet@demon.co.uk
-
- In article <1993Jan21.054036.27343@hparc0.aus.hp.com> peterj@hparc0.aus.hp.com (Peter Westley) writes:
- > I am trying to create a class which implements a "smart enum". This enum
- > type will be able recognise itself as both an int (or other integral type)
- > or as char* and also have stndard conversions to both int and char* for
- > things sunch as stream insertion.
- >
- > The way I am trying to implement them is with templates. Say I wanted
- > an enum class type which could take the values "Apple", "Banana" and "Pear"
- > each assigned (an arbitary) int. Basically to bahave much as an enum normally
- > does.
- ..
- > The idea being that I could declare a class based on this template which
- > I could have multiple instances of:
- >
- > char *somenames[] = {"Apple","Banana","Pear"};
- > char *morenames[] = {"Peter","John","Andrew"};
- >
- > typedef SmartEnum<somenames> FruitEnum;
- > typedef SmartEnum<morenames> BoysEnum;
- > FruitEnum aFruit();
- > FruitEnum anotherFruit();
- > BoysEnum aBoy();
- > BoysEnum anotherBoy();
- >
- > Then I could do things like:
- > aFruit = "Banana";
- > anotherFruit = 1;
- You don't want to do that, surely ! The whole point of enums is that you
- can't convert arbitrary integers to them (but you can convert them to
- integers).
-
- You might want to have increment and decrement operators (provided you
- could test at runtime whether they had been successful).
-
- If you output these enums to a stream in one format more often than the
- other, you might want to have an operator<< which used the default format,
- rather than always having to use a cast.
-
- Input operators are another possibility (checking for valid values, of
- course).
-
- --
- 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.
-