开发者

Conditional comparison of enum

开发者 https://www.devze.com 2023-04-05 12:27 出处:网络
Is it possible to use enums in a conditional statement, like typedef enum { kUserCapabilites_0 = 0, kUserCapabilites_1 = 1,

Is it possible to use enums in a conditional statement, like

typedef enum 
{
    kUserCapabilites_0 = 0,
    kUserCapabilites_1 = 1,
    kUserCapabilites_2 = 2
} UserCapabilites;

if (theUsersCapability == kUserCapabilites_2)

where theUsersCapability is one of the saved 开发者_开发问答UserCapabilites


Yes, you can compare. You should have tested instead.

Disclaimer: This should hold for Objective-C too. Please check it.

#include <stdio.h>

typedef enum{
   a = 1, b, c
}aa;

int main(){
    aa obj = a;

    if( obj == b )
        printf("Equal");

    else
        printf("Not Equal");

    return 0;
}

Result : Not Equal

Online Result

0

精彩评论

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