Override variable inside an enumeration

In C, if we re-declare a variable inside enum, then the compiler throws an error, which "i" is updated as a different kind of character ".It Ok.

#include <stdio.h>

int i = 10;

struct S 
{ 
    enum 
    {
        i = 20
    }e; 
};

int main()
{
    printf("%d\n", i);
}

But, in C ++, if we redefine a variable inside enum, then it works fine.

#include <iostream>
using namespace std;

int i = 10;

struct S 
{ 
    enum 
    {
        i = 20
    }e; 
};

int main()
{
    cout<<i<<endl;
}

I do not understand why the C ++ compiler does not give an error for the receclaration variable?

+6
source share
1 answer

It does not give a re-declaration error because the enumerator is being entered into the class scope. Recall that a structure and a class are basically interchangeable in C ++. The volume Scontains an enumerator i.

C struct S . C: , , 4 . , i , i.

+10

All Articles