0

In my application i want to represent AND as 1 , and OR as 0, instead of opting for #define method , i am using enum to represent them , as shown in the below example program

#include <stdio.h>

enum gateConnection_t
{AND,OR}
gateConnection;

int main()
{

bool and_t = AND;
bool or_t = OR;

printf("%d\n",and_t);
printf("%d\n",or_t);

return 0;
}

As seen above , i am directly assigning the enum values to boolean vvariables. The program works as expected and my only question is whether the internal cast done is safe , or is it better to use explicit casting such as static_cast ?

4

2 回答 2

3

首先,您的 {AND,OR} 是错误的。

标准枚举从 0 开始(尽管您可以覆盖它)。

利用

enum gateConnection_t
{OR,AND}

反而。

顺便说一句,演员是安全的。

从概念上讲,虽然这是一件令人讨厌的事情。你为什么要这个;特别是在 C++ 中?真假有什么错?

于 2013-06-11T13:34:07.383 回答
1
enum gateConnection_t
{AND=1,OR=0}
gateConnection;

或者

enum gateConnection_t
{
OR=0,
AND  //<== this equal to the previous plus one automatically 
}gateConnection;
于 2013-06-11T13:37:14.727 回答