C++ Trickery : output actual enum, not its value [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++: Print out enum value as text
Say I have an enumeration:
typedef enum MyEnum_
{
MY_ENUM_BLUE,
MY_ENUM_RED,
MY_ENUM_GREEN
} MyEnum;
And that I have a method that gets an "MyEnum" as parameter. I'd like write to log file the value of that MyEnum, but I don't want 0, 1 or 2 to appear there--- I'd like to output the actual enumeration string "MY_ENUM_xxxx", of course without a switch/case block..... and dealing with each value individually.
Is there some trickery or pattern, in c++ or using macros that would help me achieve that?

No need to typedef in c++
enum MyEnum_
{
MY_ENUM_BLUE,
MY_ENUM_RED,
MY_ENUM_GREEN
} MyEnum;
#define TOSTR(x) #x
const char *names[]={TOSTR(MY_ENUM_BLUE),
TOSTR(MY_ENUM_RED),
TOSTR(MY_ENUM_GREEN)};
int main(int argc, char *argv[])
{
MyEnum_ e=MY_ENUM_RED;
std::cout << names[e] << std::endl;
return 0;
}

Related

Can't cast type int to char using function parameters C++ [duplicate]

This question already has answers here:
Convert an int to ASCII character
(11 answers)
Closed 6 months ago.
Hello im trying to understand why i can't cast type int
To a char using a user defined function with parameters.
Im following along with learncpp. And i am a beginner,
So please could i have the simplified versions.
If i create a user function, And try a return the value back it will just output the integer instead of an ASCII character.
Here is my following code.
int ascii(int y)
{
return static_cast<char>(y);
}
int main()
{
std::cout << ascii(5) << std::endl;
return 0;
}
The issue is the type of your return value. It should be a char. Not an int
char ascii(int y)
{
return static_cast<char>(y);
}

What happens if we just use "signed" and "unsigned" without any primitive data types? [duplicate]

This question already has answers here:
Unsigned keyword in C++
(5 answers)
Closed 4 years ago.
What happens if we do not put an int or char after unsigned and signed variables?
#include<iostream>
using namespace std;
int main()
{
unsigned u = 10; // how is this declaration helpful?
signed s = 12; // is there any use with such declarations without specifying a primitive datatype
cout << u << " " << s << endl; // prints the values of the variables as usual
return 0;
}
signed and unsigned implicitly mean signed int and unsiged int, respectively.

Why does casting an int to a strongly typed enum compile if it is "not within the enums range"? [duplicate]

This question already has an answer here:
What happens if you static_cast invalid value to enum class?
(1 answer)
Closed 5 years ago.
In the code below, I use static_cast to convert an strongly typed enum to an int. The same works in the other direction. But it also works if the cast int is not within the range of the enum. Why is that and why is the compiler not catching this?
#include <iostream>
#include <string>
enum class Name {Hans, Peter, Georg}; // 0, 1, 2
std::string getName(Name name) {
switch(name) {
case Name::Hans: return "Hans";
case Name::Peter: return "Peter";
case Name::Georg: return "Georg";
default: return "not valid name";
}
}
int main()
{
// Cast a Name to an int, works fine.
std::cout<< static_cast<int>( Name::Peter ) <<std::endl; // 1
std::cout<< static_cast<int>( Name::Hans ) <<std::endl; // 0
// Cast an int to a Name
std::cout<< getName(static_cast<Name>(2)) <<std::endl; // Georg
std::cout<< getName(static_cast<Name>(3)) <<std::endl; // not a valid name
// I would expect a compiler error/warning like i get here:
// std::cout<< static_cast<int>( Name::Hans + 4 ) <<std::endl;
}
For one thing, people often use enums to represent bit flags:
enum class FontFlags { bright=0x1, bold=0x2, blink=0x4 };
Now they expect this to work:
FontFlags(int(FontFlags::bold) | int(FontFlags::blink))
But of course the value is 6 which is "impossible."

How do you use a string or char to identify a variable by name in C++? [duplicate]

This question already has answers here:
C++ conversion from string to object identifier [duplicate]
(3 answers)
Closed 6 years ago.
What I mean is, how do I use a char, which I can iterate, to identify a variable?
So if:
int cheese = 1337;
string identifier = "cheese";
How do I use this string "identifier" to identify the variable cheese and return its value?
You don't.
Instead, you lay out your data differently, perhaps using a key-value store?
std::map<std::string, int> myData;
myData["cheese"] = 1337;
// ...
const std::string identifier = "cheese";
std::cout << myData[identifier] << '\n'; // 1337
Looks like you are looking for map
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> vars;
vars.insert(make_pair("cheese", 1337));
vars.insert(make_pair("geese", 1338));
if (vars.find("cheese") != vars.end())
cout << vars.at("cheese");
return 0;
}

why size of a struct with only a bit is 4 byte but not 1 byte [duplicate]

This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Closed 7 years ago.
I think it should be 1. Because 1 byte is enough to hold this BOOL. And if I comment "BOOL b:1", it becomes a empty struct, and its size is 1 now.
#include <iostream>
#include <string>
using namespace std;
enum BOOL { FALSE=0, TRUE=1 };
struct A {
BOOL b:1;
};
int main()
{
std::cout << sizeof(A) << std::endl; //output 4.
}
Padding.
If you want the struct to be "packed" (good for memory use, TERRIBLE for performance), you can ask for that in a compiler-specific nonstandard way.