Brook Julias
Brook Julias

Reputation: 2105

Is it possible to pass an entire enum set as a parameter?

I am aware that you can use part of an enum as a parameter for a function. The question I have is can you use an entire enum as a parameter?

For the enum:

enum exampleEnum {ONE,TWO,THREE}

by partial enum I am referring to:

function example(exampleEnum value){}

function example(ONE);

by entire enum is:

function example(enum value){}

function example(exampleEnum);

I guess what I am asking is can I pass an enum like you pass an array. At least that is what I think I am asking.

edit

The effect I am trying to achieve is to share an enum across multiple classes and subclasses without redefining it in every class/subclass I wish to use it in. I want these values to be passed instead of using some form a global variable.

edit of the edit

To be more specific... I am using the enum values as a form of associative array.

enum attribute{STR,DEX,CON,INT,WIS,CHA};
short int charAttributes[6];

charAttributes[STR] = sumValue;
charAttributes[DEX] = sumValue;
charAttributes[CON] = sumValue;
charAttributes[INT] = sumValue;
charAttributes[WIS] = sumValue;
charAttributes[CHA] = sumValue;

What I am wanting is to pass the enumeration in its entirety name, values, everything to be passed as a parameter. I am wanting to pass the enumeration to keep the enumeration names and values to continue using them as such.

Upvotes: 3

Views: 1614

Answers (5)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275720

If your enumeration is contiguous (or, if you never use = in the definition of your enum), there is an easy to do trick is to iterate over the enum.

Start with this:

enum /*class*/ Bob // class optional
{
  BeginBob,
  ONE = BeginBob, // the first entry needs an = Begin clause.
  TWO,
  THREE,
  EndBob
};

now, you can pass in a range of enum values in a similar way you'd pass an iterator range.

void doWork( Bob b );
void doWork( Bob begin, Bob end )
{
  for (Bob i = begin; i != end; i=ststic_cast<Bob>(i+1) )
    doWork( i );
}

the begin and end enum values describe a half-open range, like iterators would. So you can call doWork on the entire enum range like this:

void doWork( BeginBob, EndBob );

or, you could call it on everything up to, but not including, THREE like this:

void doWork( BeginBob, THREE );

which calls doWork on ONE and TWO.

Upvotes: 1

Evgeny Panasyuk
Evgeny Panasyuk

Reputation: 9199

The effect I am trying to achieve is to share an enum across multiple classes and subclasses without redefining it in every class/subclass I wish to use it in. I want these values to be passed instead of using some form a global variable.

Hm, you don't have to redefine it. Just place enum definition outside of these classes. And when you want to use enum values in some class, just include header with that enum.

enum exampleEnum {ONE,TWO,THREE};

class Class1
{
   void foo()
   {
       exampleEnum t=TWO; // use enum values here
   }
};

class Class2
{
   void bar()
   {
       exampleEnum t=ONE; // and here
   }
};

class Class3
{
   void some()
   {
       exampleEnum t=THREE; // and even here
   }
};

EDIT:

By doing it this way I would be adding a dependency to my classes which I am try to avoid. It's better to give something to a class then to have the class take something. While I cannot completely escape from dependencies I was hoping I might have been able to.

In that case you may use templates:

enum exampleEnum {ONE,TWO,THREE};
enum exampleEnumOther {RAZ,DVA,TRI};

template<typename Enum>
class Class1
{
   Enum member;
public:
   void foo(Enum p)
   {
       member=p;
   }
   template<typename OtherEnum>
   void bar(OtherEnum val)
   {
        OtherEnum use=val;
   }
};

int main()
{
    Class1<exampleEnum> t;
    t.foo(ONE);
    t.bar(TWO);
    t.bar(RAZ);
}

Class1 do not depend on any particular enum.

Upvotes: 1

Evgeny Panasyuk
Evgeny Panasyuk

Reputation: 9199

exampleEnum is a type, not a value. C++ way to pass type to functions is using templates:

#include <iostream>
#include <ostream>
#include <typeinfo>
using namespace std;

enum exampleEnum {ONE,TWO,THREE};

template<typename T>
void example()
{
    cout << typeid(T).name() << endl;
}

int main()
{
    example<exampleEnum>();
    return 0;
}

Upvotes: 5

Ben Jackson
Ben Jackson

Reputation: 93860

You can make template <typename T> example and specialize it on several different enums, allowing you to call example(ONE) to call code specific to exampleEnum and then (given, say enum otherEnum { EINS, ZWEI, DREI } you can call example(EINS) to get code specific to otherEnum.

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308391

If you structure your enum values properly, you can combine values with the | bit-wise or operator.

enum exampleEnum {One=0x01, TWO=0x02, THREE=0x04, FOUR=0x08}; // one bit set in each

example(ONE | TWO | FOUR);

In your function you need to test for each value individually:

if (value & ONE) // ONE was passed
if (value & TWO) // TWO was passed, etc.

Upvotes: 1

Related Questions