op.conversion enum->char

R

Rafal 'Raf256' Maj

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.
 
J

Jeffrey Schwab

Rafal said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top