Jump to content

Classes


russo12
 Share

Recommended Posts

Outra hipótese seria usar um enum. Talvez considere o uso de um enum como a melhor em termos teóricos, mas

On the other hand, there is not an implicit conversion from int to an enum type:

    MyEnumType x = 2;    // should NOT be allowed by compiler

esta é uma funcionalidade que sempre quis. Não gosto nada quando é preciso importar dados dum ficheiro onde temos números e é preciso passar para o enum.

edit: mas compreendo que a funcionalidade que referi não é exequível em C++

"What we do for ourselves dies with us. What we do for others and the world, remains and is immortal.", Albert Pine

Blog pessoal : contém alguns puzzles, algoritmos e problemas para se resolver com programação.

Link to comment
Share on other sites

Podes utilizar

MyEnumType met = static_cast<MyEnumType>(2);

embora isto não faça muito sentido, se à partida sabes qual o valor que queres dar à variávell, e podes usar o valor definido na enumeração.

Utilizando uma variável, o código fará mais sentido. Tens é que ter código para validar o valor inteiro que vais atribuir à variável, algo assim:

enum MyEnumType {
INDEFINIDO = -1,
ZERO = 0,
UM,
DOIS,
TRES,
QUATRO
};

int i = 2;

// ...

MyEnumType met = (i <= QUATRO && i >= 0 ? static_cast<MyEnumType>(i) : INDEFINIDO);

Nota: um tipo enum é automaticamente convertido para int quando usado desta forma.

Desaparecido.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.