开发者

C++ enumeration

my question is about enumeration,my codes are :

#include<iostream>
using namespace std;
int main()
{
    enum bolumler {programcilik,donanim,muhasebe,motor,buro} bolum;
    bolum = donanim;
    cout << bolum << endl;
    bolum += 2;  // bolum=m开发者_如何学Cotor
    cout << bolum;

    return 0;
}

The output should be 1 3 but according to these codes the error is:

 error C2676: binary '+=' : 'enum main::bolumler' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.

111.obj - 1 error(s), 0 warning(s)

Can you help me ?The other question is what can I do if I want to see the output like that "muhasebe"?


By definition, you can't do this kind of arithmetic on a enum type. If you need it, write your own += operator, like this:

bolumler& operator+=(bolumler& v1, int v2) {
    return v1 = bolumler(int(v1) + v2);
}


You can't use += on variables of an enum type unless you define operator+=.

If you really needed to you could add 2 and then cast back to the enum type, but this is not very nice code.

bolum = (bolumler)(bolum + 2); /* bolum=motor */

About your other question:

111.obj - 1 error(s), 0 warning(s) Can you help me ?The other question is what can I do if I want to see the output like that "muhasebe"?

You would need to manually convert the enum variable to a string with a switch statement.

std::string enum_to_string(bolumler val)
{
    swtich(val)
    {
       case programcilik:
         return "programcilik";
      //....


An enum is not of type int, although the values are substituted by numeric vals. Thus, you cannot use operators on it. Use integers instead of an enum to make it work.

For string output, you have to use strings. A descriptor's name cannot be displayed as string. Descriptors are often stripped completely when compiling your code anyway, so you can't even find them after disassembling the program.


The problem is that enums can implicitly convert to int, but int can't implicitly convert to enum types.

When you add 2 to an enum, the result is an int or unsigned int (or possibly some other integer type, if the enum contains really big values), which isn't allowed to be assigned back to an enum without a cast. To preserve this restriction, there's also no += operator on enums.

You could declare bolum as an int, instead of being of enum type, or you could write something like:

bolum = static_cast<bolumler>(bolum+2);

But beware that this is a bit risky. bolum+2 is still in the valid range of the enumerated type bolumler, and actually so is buro+2 for technical reasons to do with the exact way that the range of an enum is defined. But in general you can't just go adding 2 to an enum value and expect it necessarily to still be in range of the enum. The sole purpose of using an enumerated type instead of an int is to get extra type safety: if you don't want that, then just define a bunch of integer constants for the values you need.


The real problem is that what you are trying to achieve does not make sense in the context of an enum.

You could substitute any value instead of 2, but what happens if the value you get back does not correspond to any value of the enum ? It's undefined...

I've run into the problem a couple of times, notably it seems perfectly reasonable to iterate over the values of an enum... The only solution I found was to stash the values into a container (vector for example) and then iterate over the container instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜