Enum type check

keshav yadav keshav.yadav2005@gmail.com
Tue Jun 15 14:56:00 GMT 2010


Hi Andrew,

 Thanks for reply  below the reasion why we are getting the error
In C, an enum is basically a way to alias names to integers.This does
not lead to improved type checking. In C++, an enum defines an actual
type, which results in strong type checking.
In C++, an integer cannot be assigned directly to an "enum" as it
could be in C. If your program contains such assignments, first cast
the integer to the "enum" type, as in the following example:
for example:-
int i;
enum X {a, b, c} e;
e = i; /* Legal in C, not in C++. */
e = (enum X)i; // Legal in C++.

   What I want to know is how to catch an error on C code. is there
any flag in gcc with we can get the same error .


thanks



On Tue, Jun 15, 2010 at 2:04 PM, Andrew Haley <aph@redhat.com> wrote:
> On 06/15/2010 04:56 AM, naveen yadav wrote:
>> Hi all ,
>>
>> If we compile attached c file (enum.c), build result is different
>> between C & C++ compiler.
>>
>> example:
>> [naveen@localhost ~]$ gcc enum.c
>> [naveen@localhost ~]$ g++ enum.c
>> enum.c: In function āint main()ā:
>> enum.c:13: error: cannot convert ātypeBā to ātypeAā in assignment
>> [naveen@localhost ~]$
>
> C++ has stronger type checking.  You need
>
>  a= (typeA)B2;
>
> Andrew.
>



More information about the Gcc-help mailing list