views:

298

answers:

6

If you're supposed to encapsulate everything inside a class definition, how is it then possible to use enumerated data types with the class? For example I've just written the following code...

enum PizzaType {DEEP_DISH, HAND_TOSSED, PAN};
enum PizzaSize {SMALL, MEDIUM, LARGE};

class Pizza {
    public:
        Pizza();
        void setPizzaType(PizzaType type);
        PizzaType getPizzaType();
        void setPizzaSize(PizzaSize size);
        PizzaSize getPizzaSize();
        void setToppings(int toppings);
        int getToppings();
        void outputDescription();
        double computePrice();
    private:
        PizzaType pizzaType;
        PizzaSize pizzaSize;
        int totalToppings;
};

Is there any way of including the enumerated data types inside the class itself and yet still allow access to the mutator/accessor functions from outside?

+1  A: 

That is not a problem, you can create variables of this enum type by specifying Pizza::PizzaType.

Put the enums inside your class, if you want them to be accessed by the outside make sure they are public too.

Pizza p;
p.setPizzaType(Pizza::PAN);
Pizza::PizzaType pt = p.getPizzaType();
assert(pt == Pizza::PAN);
Brian R. Bondy
+1  A: 

As long as the enumeration type is public, you are allowed to use it outside the class. You just have to scope it i: Pizza::PizzaType. The same rule exists for static methods and for static constants and variables.

Uri
+1  A: 

You can define the enum inside the class and use it outside like how you use a namespace i.e. classname::enum_type. Actually it is a good idea to define it inside the class as it avoids the global namespace pollution.

Naveen
+3  A: 
class Pizza {
    public:
        enum PizzaType {DEEP_DISH, HAND_TOSSED, PAN};
        void setPizzaType(PizzaType type);
        PizzaType getPizzaType();
};
...
Pizza p;
p.setPizzaType( Pizza::DEEP_DISH );
Pizza::PizzaType pt = p.getPizzaType();
anon
+5  A: 

Yes, you can do it like this:

class Pizza {
    public:
        enum PizzaType {DEEP_DISH, HAND_TOSSED, PAN};
        enum PizzaSize {SMALL, MEDIUM, LARGE};

        Pizza();
        void setPizzaType(PizzaType type);
        PizzaType getPizzaType();
        void setPizzaSize(PizzaSize size);
        PizzaSize getPizzaSize();
        void setToppings(int toppings);
        int getToppings();
        void outputDescription();
        double computePrice();
    private:
        PizzaType pizzaType;
        PizzaSize pizzaSize;
        int totalToppings;
};

.. and then others have said, you just have to use the Pizza "namespace" in order to get to the enum types:

Pizza::PizzaType tmp = pPizza->getPizzaType();

etc, etc.

(As a note of style, when you put the enums inside of the class like this, I would personally remove the Pizza in front of it so that you have Pizza::Type and Pizza::Size.)

Jim Buck
Your enums are actually private, so others can't declare variables of those types. Move their declarations into the public section.
j_random_hacker
Yep, I got cut'n'paste happy. Thanks for the edit, dribeas.
Jim Buck
+3  A: 

The preferred method of using enums in C++ is to define them inside a class:

class Foo {
public:
     enum Bar {
         ENUM_VALUE1,
         ENUM_VALUE2
     };
};

Then, you can reference them using:

Foo::Bar var;
var = Foo::ENUM_VALUE1;

Inside the class, you can drop the Foo:: prefix.

As you have probably noticed, while the type of the enum is Foo::Bar, the values are not referenced through Foo::Bar::ENUM_VALUE1, but rather appear in the Foo's namespace. This may be problematic if different enums share the same value names. To avoid that, you can do the following trick:

class Foo {
public:
      struct Bar {
             enum ENUM {
                  ENUM_VALUE1,
                  ENUM_VALUE2
             };
      struct Baz {
             enum ENUM {
                  ENUM_VALUE1,
                  ENUM_VALUE2
             };
      };
};
Foo::Bar::ENUM e = Foo::Bar::ENUM_VALUE1;
Foo::Baz::ENUM e2 = Foo::Baz::ENUM_VALUE1;
ASk
+1 for the struct { enum } tip. BTW, you are missing a }; for struct Bar
Rado