Is there any way to overload "typecasting"

R

Raghu

Hello All,

I need some help regarding overloading operation.
Is there any way to overload typecasting?
I mean if i have a piece of code as below.

int a = 2:
float b;

b = (float)a;

Here during typecasting i want to convert the integer to the IEE754 single
precision format float value.
During this process of conversion i want to use specific algorithm instead
of the way how compiler does.

Please post your valuable ideas at the earliest.

Thanks and Best Regards
Raghu.
 
M

mlimber

Raghu said:
Hello All,

I need some help regarding overloading operation.
Is there any way to overload typecasting?
I mean if i have a piece of code as below.

int a = 2:
float b;

b = (float)a;

Here during typecasting i want to convert the integer to the IEE754 single
precision format float value.
During this process of conversion i want to use specific algorithm instead
of the way how compiler does.

Please post your valuable ideas at the earliest.

Thanks and Best Regards
Raghu.

You can overload constructors and conversion operators for classes:

struct MyFloat
{
MyFloat( int i ) { /* Perform conversion from int */ }

operator int()
{
const int i = <Do some conversion to int here>;
return i;
}
// ...
};

Cheers! --M
 
H

Howard

mlimber said:
You can overload constructors and conversion operators for classes:

struct MyFloat
{
MyFloat( int i ) { /* Perform conversion from int */ }

operator int()
{
const int i = <Do some conversion to int here>;
return i;
}
// ...
};

Quick follow-up:

Will that conversion operator be called for both of the following casts?

MyFloat x = 1.23;
int a = (int)x;
int b = int(x);

?

-Howard
 
O

Old Wolf

Howard said:
Will that conversion operator be called for both of the following casts?

MyFloat x = 1.23;
int a = (int)x;
int b = int(x);

Yes, and it will also be called for:
int c = x;

It applies to any conversion from MyFloat to int, regardless of how
that conversion was specified.
 
M

Michiel.Salters

Howard said:
Quick follow-up:

Will that conversion operator be called for both of the following casts?

MyFloat x = 1.23;
int a = (int)x;
int b = int(x);

That's three casts, and I think the logical ctor MyFloat::MyFloat(
double d )
is indeed missing.

HTH,
Michiel Salters
 
H

Howard

That's three casts, and I think the logical ctor MyFloat::MyFloat(
double d )
is indeed missing.
Oops! Forgot about how to properly initialize x. But my question was about
the folowing two lines (and Old Wolf answered that already).

Thanks,
Howard
 
H

Howard

Old Wolf said:
Yes, and it will also be called for:
int c = x;

It applies to any conversion from MyFloat to int, regardless of how
that conversion was specified.

Ok, thanks.
-Howard
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top