I have an enum type, for example:

enum signum {negative = -1, positive = 1, zero = 0, inward = -1, outward = 1, indefinite = 2};

I want to use at least one of the following castings:

signum s = positive;
double x,y;
x = double(s);
y = (double)s;

Recommended Answers

All 9 Replies

Worked ok with my compiler (vc++ 2010 express)

Worked OK? But what?
I do not define anything!
It was just a simple example,
but what if I want to convert other floating point numbers,
nut just 0, 1, -1,
for example I want to convert signum == 2 -> double NaN,
so I want to define a casting function:
double(signum s)

I mean: user defined casting, not the default!

Worked OK? But what?
I do not define anything!
It was just a simple example,

Sure you did

#include <iostream>

enum signum {negative = -1, positive = 1, zero = 0, inward = -1, outward = 1, indefinite = 2};

int main()
{

signum s = positive;
double x,y;
x = double(s);
y = (double)s;

}

>so I want to define a casting function:
>double(signum s)

So do it. We're not talking about rocket science here:

double to_double(signum s);

Or are you going to complain that double(s) is so vastly superior to to_double(s) that you absolutely must have a user-defined cast that uses exactly the same syntax as the native cast? :icon_rolleyes:

commented: Quite. +20

It is not possible to define "real" casting?
I don't want to define to_double function.
Instead of enum, the casting of a class is allowed, as I know,
I can just define member operator double();

>It is not possible to define "real" casting?
What I see here is the difference between a professional attitude and an amateur attitude. Yes, it is possible, but it's less clear, takes longer to implement, requires more effort to maintain, and doesn't really buy you anything for the added complexity. One with an amateur attitude will do it anyway while one with a professional attitude will crank out something simple in a few minutes (a function, perhaps?) and spend valuable time on more important things.

Can you see the difference?

Yes, I can, I understand you, but please answer me, what is the syntax to do it my way?

>what is the syntax to do it my way?
Write a class that simulates an enumeration and overload the double operator for implicit conversions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.