A case for unnamed enums

This is a discussion on A case for unnamed enums within the C++ Programming forums, part of the General Programming Boards category; Need your help deciding if this is an acceptable use of an unnamed enum. CAttributes is shown bellow. It's a ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510

    A case for unnamed enums

    Need your help deciding if this is an acceptable use of an unnamed enum.

    CAttributes is shown bellow. It's a class responsible for storing, managing and displaying a set of attributes from a monster, player or npc character. The sole data member is an unsigned long with the packed value of these attributes. Attributes range from 1-20 (5 bits). There are 6 attributes.

    Code:
    namespace attributes {
    
    class CAttributes {
    public:
        explicit CAttributes(const unsigned long val): attrs_(val) {}
        ~CAttributes() {}
    
        unsigned long get();
        void change(int, unsigned int);
    
        unsigned int strength() const;
        unsigned int dexterity() const;
        unsigned int constitution() const;
        unsigned int intelligence() const;
        unsigned int smartness() const;
        unsigned int sanity() const;
    
    private:
        unsigned long attrs_;
    };
    
    enum { STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, SMARTNESS, SANITY };
    
    /* Non members */
    unsigned int strength(unsigned long);
    unsigned int dexterity(unsigned long);
    unsigned int constitution(unsigned long);
    unsigned int intelligence(unsigned long);
    unsigned int smartness(unsigned long);
    unsigned int sanity(unsigned long);
    
    unsigned long genAttrib(unsigned int, unsigned int);
    
    }
    The unnamed enum is as the second argument to the void change(int val, unsigned int at) member function. val is how much the attribute is to be added or subtracted, at is what attribute is to be changed. Inside the function there exists a switch based on at.

    A user of the class can thus use the class as this:

    Code:
     // Instantiate a CAttributes with random attributes on the range [10,18]
    attributes::CAttributes myattrs(genAttrib(10,18))
    
    myattrs.change(2, attributes::STRENGHT);  // add 2 to strenght;
    myattrs.change(-1, attributes::DEXTERITY);  // subtract 1 to desxterity;
    Is this an acceptable use for an unnamed enum considering the needs of the change() function, as an alternative to have 6 member functions, one for each attribute?

    On that case would you have it declared as non-member or member? Seems to me I add nothing by declaring it a member and I complicate things further by forcing yet another scope on anyone wanting to use it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is this an acceptable use for an unnamed enum
    I don't see why not.

    >On that case would you have it declared as non-member or member?
    I think a non-member makes more sense.
    My best code is written with the delete key.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's fine as an unnamed enum, but don't you think it would be even better to name the enum and make the first argument of the function of that type? Then you get immediate documentation about what the user is supposed to pass there, and you get compiler-enforced checking.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Absolutely!

    I wasn't imagining a named attribute. For some reason I thought that then I would have to create an object before using it.

    Thanks both.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 01:08 PM
  5. returning to a spot in the code
    By Shadow in forum C Programming
    Replies: 4
    Last Post: 09-22-2001, 05:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21