3

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.

3
  • ONE is a value which can be passed as a function argument. exampleEnum is a type, which cannot. If it were possible, what you expect your function to do with it? Commented Oct 27, 2012 at 0:16
  • 1
    I was planning on passing it to a class constructor so as to have all classes/subclasses working with the same data without having to declare it over and over. Commented Oct 27, 2012 at 1:36
  • You probably want to be more clear about what you are doing. You can pass types to a class at compile time via templates, but depending on what your goal is and how you intend to do it this may not be practical. Commented Oct 27, 2012 at 20:07

5 Answers 5

5

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that typeid().name() is not guaranteed to be "exampleEnum" and may have other compiler-specific gunk in it. You will probably want to filter it somewhat before printing it.
1

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.

Comments

1

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.

2 Comments

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.
I have added example without dependencies.
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.