Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Is there any way to overload "typecasting" (http://www.velocityreviews.com/forums/t450376-is-there-any-way-to-overload-typecasting.html)

Raghu 12-08-2005 12:02 PM

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.






mlimber 12-08-2005 01:05 PM

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


mlimber 12-08-2005 01:12 PM

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


Howard 12-08-2005 11:11 PM

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




Old Wolf 12-09-2005 12:09 AM

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.


Michiel.Salters@tomtom.com 12-09-2005 02:07 PM

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


Howard 12-09-2005 03:31 PM

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




Howard 12-09-2005 03:31 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57