By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,147 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,147 IT Pros & Developers. It's quick & easy.

op.conversion enum->char

P: n/a
Hi,
how can I make operator that will automaticly converse tMyEnum into char,

typedef enum { ... } tMyEnum;
void Fun(char c) { ... }

tMyEnum e;
Fun( e );

so that the last line - function call can be more natural then:

Fun( (tMyEnum)e );

is converting enum into class the only possibility for doing so, assuming
that I do NOT want to overload Fun() with Fun(tMyEnum e) - because there
are several functions, with more arguments etc.

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~l-.~~~~~~~~~~~~~~~~~~~
GG-1175498 ____| ]____,
Rafal 'Raf256' Maj X-( * )
Rafal(at)Raf256(dot)com ,"----------"
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Rafal 'Raf256' Maj wrote:
Hi,
how can I make operator that will automaticly converse tMyEnum into char,

typedef enum { ... } tMyEnum;
You may get better error messages by giving that enum a tag, instead of
using typedef.

enum tMyEnum { /* ... */ };
void Fun(char c) { ... }

tMyEnum e;
Fun( e );
<cringe> Why would you do that? </cringe> If Fun was intended to take
a char, why are you passing it an enum? If it was always the intention
that Fun would take an enum, why was it declared to take a char?

so that the last line - function call can be more natural then:

Fun( (tMyEnum)e );
Do you mean (char)e? That variable is already a tMyEnum.

Anyway, I think this looks a little better:

Fun( char( tMyEnum ) );

I have to say that if you're really hellbent on casting an enum to a
char <residual cringe/>, it might actually be one of those rare cases
where an explicit cast will make the code more readable.
is converting enum into class the only possibility for doing so, assuming
that I do NOT want to overload Fun() with Fun(tMyEnum e) - because there
are several functions, with more arguments etc.


Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.