Is this code Right?

Well:

1
2
3
4
  enum MyEnum{a, b, c, d, e};
int main()
{
 std::cout << a;

just works fine, it prints 0;

Is it also possible to declare an enumeration globally and just use it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
enum lewd{PLUS, MINUS};

struct lawd
{
    int a;
    int &operator()(lewd lw, int value)
    {
        if(lw == PLUS)
        {
            a+=value;
        }
    }
};

int main(int argc, char* argv[])
{
    lawd lel;
    lel.a = 1;
    lel(PLUS, 2);
    cout << lel.a << endl;
}
Last edited on
Unless otherwise specified, the default value for the first enumerated value should be 0: http://www.cprogramming.com/tutorial/enum.html

you can choose to use default values, which start at zero for the first constant


1
2
3
4
5
6
7
8
9
#include <iostream>

enum MyEnum {a, b, c, d, e};

int main()
{
	std::cout<< "a: "<< a<< '\n';
	return 0;
}
a: 0
But for istance if i do this
1
2
3
4
5
6
enum MyEnum{a, b, c}
int main()
{
 int a = 0;
 std::cout << a;
}


What variabile is going to use and why is going to use?

and also in the second code how the operator() doesn't need a declared type like

1
2
3
4
5
6
int main()
{
 lewd lw
 lawd lul = PLUS) // shouldn't the first code i posted uncompile?
 lewd(lul, 2);
}
What variabile is going to use and why is going to use?

Since you declare a variable a in the local scope of main, it shadows (hides) MyEnum::a:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

enum MyEnum {a, b, c, d, e};

int main()
{
	int a = 10;
	std::cout<< "a: "<< a<< '\n';         // local a
	std::cout<< "a: "<< MyEnum::a<< '\n'; // MyEnum::a
	std::cout<< "a: "<< ::a<< '\n';       // MyEnum::a
	return 0;
}
a: 10
a: 0
a: 0


and also in the second code how the operator() doesn't need a declared type like

I'm not quite sure what it is you are asking, could you possibly rephrase the question?

shouldn't the first code i posted uncompile?

Maybe you mean not compile? For me it won't because your operator() function is missing a return statement.
I mean

1
2
3
4
5
6
7
8
9
10
11
12
enum MyEnum{a, b, c, d}
struct MyStruct
{
 int a;
 int &operator()(MyEnum)
};

int main()
{
 MyStruct mystruct;
 MyStruct(b); //This shouldn't work right? Beacuse i've never declared a variabile of type MyEnum like MyEnum myenum = b;
}

Last edited on
I may be wrong, but I think what that line is doing is creating an instance of type MyStruct named b.
> Is it also possible to declare an enumeration globally and just use it like this:

Yes. (Provided a return statement is added to the overloaded operator).
http://coliru.stacked-crooked.com/a/46c25c65f41b49f2
Topic archived. No new replies allowed.