enum scope

V

vsgdp

Hi,

Do enumerators have scope?

class A
{
};
enum E {A, B, C};
int main()
{
E e = E::A;
}

Can ::A be distinguished from E::A?
 
J

John Carson

vsgdp said:
Hi,

Do enumerators have scope?

class A
{
};
enum E {A, B, C};
int main()
{
E e = E::A;
}

Can ::A be distinguished from E::A?

There is no such thing as E::A. Enum values do have scope, but what that
means is the following:

enum Outer {A=0};

int main()
{
enum Inner {A=1};
int x = A; // x is 1, not 0
}
 
R

Ron Natalie

vsgdp said:
Hi,

Do enumerators have scope?

class A
{
};
enum E {A, B, C};
int main()
{
E e = E::A;
}

Can ::A be distinguished from E::A?
The enumerators have the scope of where the enum is declared. In
this case at namespace scope. The class A and the enumerator A
are in the same namespace scope.

You could wrap a namespace around your enumerator if you wanted
differentiate:
class A ;
namespace E {
enum T { A, B, C };
};

E::T e = E::A;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top