Thread: Enum and body parts

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    Enum and body parts

    Hello everyone,

    I'm somewhat new to C++. I hope you can help me with the following.

    I have a base class called CItem. I also have a global scope enum...

    PHP Code:
    enum Body_Part NAHeadNeckBodyLeft_ArmRight_ArmWaistLegsFeet }; 
    CItem will have a data member of Body_Part type that will describe where the item can be worn. It is defined globally because the CPlayer class will also use it.

    The problem I'm having is twofold:

    How can I describe an item that can be used in more than one body part?
    I thought of a vector of Body_Part elements. But isn't this overwhelming?

    And,

    A Right_Arm body item can also be used on the Left_Arm. How can I make this happen? I guess perhaps the first question will also answer this one.

    Thank you

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The vector is certainly a valid option. It might seem like overkill, but it would work and it would be clear, which should always be your first priority. Another option that is probably better is to use a bitset. You could still use the enum (although NA wouldn't be needed) and turn on and off whichever bits are allowed for that item.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > A Right_Arm body item can also be used on the Left_Arm
    Arms are symmetrical, hands are not.
    So why not just have "Arms", like you have "Legs" ?

    Gloves (and boots - for your left and right foot) definitely come in left and right flavours.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    It will in fact be possible to wear, for instance, a band on the right arm, and another different band on the right. However, I'm not after the type of detail that would mean the player finding just a left foot boot

    I like the bitset option. You mean constructing a bitset with a size equal to the number of body parts. Each position in the bitset would correspond to the same position in the enum. Correct?

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Aye, so
    Code:
    enum Body_Part {Head, Neck, Body, Left_Arm, Right_Arm, Waist, Legs, Feet };
    and you want it to be left arm/right arm would be like:

    00011000

    If I understand what he said.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Correct?
    Correct. The number of possible body parts is known at compile time, so that is the value you use in the template. I might do this:
    Code:
    enum Body_Part {Head, Neck, Body, Left_Arm, Right_Arm, Waist, Legs, Feet, Num_Body_Parts };
    ...
    std::bitset<Num_Body_Parts> valid_body_parts;
    ...
    if (valid_body_parts[Neck])
    ...

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    Wow!

    Thanks everyone
    That is perfect!

Popular pages Recent additions subscribe to a feed