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

Is there any way to overload "typecasting"

P: n/a
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.

Dec 8 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Raghu wrote:
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

Dec 8 '05 #2

P: n/a
mlimber wrote:
Raghu wrote:
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


PS, See the FAQ for why you can't overload operators for built-in
types:

http://www.parashift.com/c++-faq-lit...html#faq-26.10

Dec 8 '05 #3

P: n/a

"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Raghu wrote:
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;
}
// ...
};


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

Dec 8 '05 #4

P: n/a
Howard wrote:
"mlimber" <ml*****@gmail.com> wrote:
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;
}
};


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.

Dec 9 '05 #5

P: n/a

Howard wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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);


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

HTH,
Michiel Salters

Dec 9 '05 #6

P: n/a

<Mi*************@tomtom.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...

Howard wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
> 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);


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

Dec 9 '05 #7

P: n/a

"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Howard wrote:
"mlimber" <ml*****@gmail.com> wrote:
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;
}
};


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.


Ok, thanks.
-Howard

Dec 9 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.