Is there any way to overload "typecasting"

Discussion in 'C++' started by Raghu, Dec 8, 2005.

  1. Raghu

    Raghu Guest

    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.
     
    Raghu, Dec 8, 2005
    #1
    1. Advertising

  2. Raghu

    mlimber Guest

    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, Dec 8, 2005
    #2
    1. Advertising

  3. Raghu

    mlimber Guest

    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-lite/intrinsic-types.html#faq-26.10
     
    mlimber, Dec 8, 2005
    #3
  4. Raghu

    Howard Guest

    "mlimber" <> wrote in message
    news:...
    > 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
     
    Howard, Dec 8, 2005
    #4
  5. Raghu

    Old Wolf Guest

    Howard wrote:
    > "mlimber" <> 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.
     
    Old Wolf, Dec 9, 2005
    #5
  6. Raghu

    Guest

    Howard wrote:
    > "mlimber" <> wrote in message
    > news:...
    > > 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, 2005
    #6
  7. Raghu

    Howard Guest

    <> wrote in message
    news:...
    >
    > Howard wrote:
    >> "mlimber" <> wrote in message
    >> news:...
    >> > 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, Dec 9, 2005
    #7
  8. Raghu

    Howard Guest

    "Old Wolf" <> wrote in message
    news:...
    > Howard wrote:
    >> "mlimber" <> 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
     
    Howard, Dec 9, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    395
    Nick Hounsome
    Jan 25, 2004
  2. DaSt
    Replies:
    18
    Views:
    763
    Alf P. Steinbach
    Jul 11, 2007
  3. Eric Danstron

    Are there ANY good forums out there anymore???

    Eric Danstron, Sep 21, 2005, in forum: ASP General
    Replies:
    1
    Views:
    169
    Immediacy
    Sep 22, 2005
  4. Andries

    is there a way ..... any way

    Andries, Apr 26, 2004, in forum: Perl Misc
    Replies:
    27
    Views:
    284
    Robin
    Apr 27, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    281
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page