samedi 24 novembre 2018

Bitwise operations in Enumarators

I have a requirement to add some extra data into an existing enumeration code based on a scenario. As a sample I have the below code:

#include <iostream>

enum Permissions {Readable = 0x4, Writable = 0x2, Executable = 0x1};

int main()
{
Permissions perms = static_cast<Permissions>(Executable | static_cast<Permissions>(29));
std::cout << perms  <<  std::endl;
perms &= ~Permissions::Executable;
std::cout << perms  <<  std::endl;
}

At first I am trying to add the data and later extract the same - but I get compilation error:

$ c++ -std=c++11 try67.cpp
try67.cpp: In function 'int main()':
try67.cpp:9:7: error: invalid conversion from 'int' to 'Permissions' [-fpermissive]
 perms &= ~Permissions::Executable;

Is the approach correct and how can we remove the compilation error?

Aucun commentaire:

Enregistrer un commentaire