![]() |
Is there any way to overload "typecasting"
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. |
Re: Is there any way to overload "typecasting"
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 |
Re: Is there any way to overload "typecasting"
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 |
Re: Is there any way to overload "typecasting"
"mlimber" <mlimber@gmail.com> wrote in message news:1134047111.703312.172500@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 |
Re: Is there any way to overload "typecasting"
Howard wrote:
> "mlimber" <mlimber@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. |
Re: Is there any way to overload "typecasting"
Howard wrote: > "mlimber" <mlimber@gmail.com> wrote in message > news:1134047111.703312.172500@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 |
Re: Is there any way to overload "typecasting"
<Michiel.Salters@tomtom.com> wrote in message news:1134137265.615101.268880@g43g2000cwa.googlegr oups.com... > > Howard wrote: >> "mlimber" <mlimber@gmail.com> wrote in message >> news:1134047111.703312.172500@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 |
Re: Is there any way to overload "typecasting"
"Old Wolf" <oldwolf@inspire.net.nz> wrote in message news:1134086956.541615.94570@o13g2000cwo.googlegro ups.com... > Howard wrote: >> "mlimber" <mlimber@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 |
| All times are GMT. The time now is 05:01 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.