Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 33,324 of 33,346    |
|    Francis Glassborow to MARH    |
|    Re: Class to behave like ints    |
|    09 May 14 15:54:27    |
   
   From: Francis.glassborow@btinternet.com   
      
   On 08/05/2014 13:47, MARH wrote:   
   > { double-linefeed quoting, quoted banner, lines longer than 70 chars   
   > corrected by moderator. please pay attention to formatting.   
   > thank you. -mod }   
   >   
   > On Thursday, May 8, 2014 1:11:32 AM UTC+1, MARH wrote:   
   >> I have a need for a class to be used as a type of ID, say an ID for   
   >> apples, I also need another class for IDs for oranges, another for   
   >> pears and so on. I also need it to be the case that you cannot assign   
   >> an apple ID to a pear ID. For example:   
   >>   
   >> AppleID a(7); // ID for an apple.   
   >> AppleID b;   
   >>   
   >> b = a; // Allowed.   
   >>   
   >> PearID p(5); // ID for a pear obviously.   
   >>   
   >> b = p; // Oops, not allowed. Would like to be a compile error.   
   >>   
   >> I recall reading about a templated class that gave this behaviour,   
   >> where some sort of signature differentiated between what effectively   
   >> were the same class. Unfortunately my memory has failed me and my   
   >> googling skills are inadequate. Does anyone have suggestion or can   
   >> point me in the right direction?   
   >>   
   >> Thank you.   
   >>   
   >   
   > Perhaps I haven't made myself clear, I have a whole set of apples,   
   > with ids; for example:   
   >   
   > AppleID a(7), b(99), c(33);   
   >   
   > And likewise pears with their own ids:   
   >   
   > PearID p(7), q(8), r(1000000)   
   >   
   > and I don't want to be able to compile:   
   >   
   > a = p; // Bad!   
   > a = b; // Good.   
   >   
   > As these are IDs I don't want or need the usual arithmetic operators.   
   > As I ascribe new IDs it's useful to be able to write:   
   >   
   > AppleID d(++a);   
   >   
   > And I need comparison operators for testing equality and sorting.   
   >   
   > I'm sorry if my original post wasn't clear enough.   
   >   
   > Thanks   
   >   
   >   
   Then use   
   enum class AppleID: unsigned int {lowest = 0, highest=999};   
   and   
   enum class PearID : unsigned int {lowest = 0, highest=999};   
      
   because these types do not have implicit conversions to the base type.   
      
   Francis   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca