Here's a code sample (there are better enum examples out there), though you might want to change the numbering (I've kept your scheme, though if feels weird starting at 1). The FINAL_DECISAO is there in case you want to use it in a loop somewhere (and should be updated to point to the right enum entry). Also, you should be careful with namespace pollution. Obs: you don't need to explicitly define each value, the enum is supposed to increment by one at each subsequent enum entry (I recommend defining the first one,as some compilers start them at 0 others at 1).
namespace Decisao
{
// FINAL_DECISAO must match the last enumerator entry
enum inertiaEnum {
Positions = 1,
Body = 2,
Context = 3,
Creditos = 4,
FINAL_DECISAO = creditos
};
//... whatever else is on this namespace
}
namespace Decisao
{
// FINAL_DECISAO must match the last enumerator entry
enum inertiaEnum {
Positions = 1,
Body = 2,
Context = 3,
Creditos = 4,
FINAL_DECISAO = creditos
};
//... whatever else is on this namespace
}
To copy to clipboard, switch view to plain text mode
You can then use the enum as you would use the hard coded int, except you won't need to remember what each number means. It will also allow you to change the numbering without refactoring the entire code.
switch (decisao){
case Decisao::Positions:
//do what should be done when Positions is selected
case Decisao::Body:
//do what should be done when Body is selected
case Decisao::Context:
//do what should be done when Context is selected
case Decisao::Creditos
//do what should be done when Creditos is selected
default:
// do what should be done when none of the others is selected
}
switch (decisao){
case Decisao::Positions:
//do what should be done when Positions is selected
case Decisao::Body:
//do what should be done when Body is selected
case Decisao::Context:
//do what should be done when Context is selected
case Decisao::Creditos
//do what should be done when Creditos is selected
default:
// do what should be done when none of the others is selected
}
To copy to clipboard, switch view to plain text mode
Qt4 replaced many of Qt3 boolean function parameters for enums so you could actually know what it meant. One such example (I'm pretty sure there was something like this in a blog post about Qt API design somewhere):
// Qt4 replacing a case sensitive == false flag for an enumerator
exampleString.replace("%USER%", user, false); // Qt 3
exampleString.replace("%USER%", user, Qt::CaseInsensitive); // Qt 4
// Qt4 replacing a case sensitive == false flag for an enumerator
exampleString.replace("%USER%", user, false); // Qt 3
exampleString.replace("%USER%", user, Qt::CaseInsensitive); // Qt 4
To copy to clipboard, switch view to plain text mode
Bookmarks