enum options {Yes,No};
class A{
int i;
string str;
options opt;
};
int main{
A obj;
obj.i=5;
obj.str="fine";
obj.opt="Yes"; // compiler error
}
How can assign const char * to opt?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just do
This code:
attempts to assign a string literal (a completely different type) to an enum type, which C++ doesn’t automagically convert for you.
You’ll have to do this manually, I like to keep a set of free functions around for doing conversions like this with my enums, ie I’ll wrap my enums in a namespace and provide some functions for working with them:
Now you can do:
So you can see, enums in C++ probably don’t give you all the bells and whistles of enums in other languages. You’ll have to manually convert things yourself.