473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

Dec 8 '05 #1
7 2122
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
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

"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
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

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

<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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
In the following loop, at the end, the method debugNotes() is printing out some notes for me to read, so I can figure out what is going on in my script. Where I try to print out the value of the...
10
by: Christian Staudenmayer | last post by:
Hi, is there any revision of the C standard that allows the "inline" keyword (or a similar feature)? I know it is possible in gcc, but then it might be a gcc feature only. Greetings, Chris ...
16
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program...
18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
11
by: Russ | last post by:
I have a couple of questions for the number crunchers out there: Does "pow(x,2)" simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)?...
3
by: SheldonMopes | last post by:
I sometimes get a pop-up box that reads "Overflow" and the module that is executing pauses. It doesn't get caught by my error trapping, and it seems to be randow. By random, I mean usually in the...
0
by: ern | last post by:
So I want to compare the longer wchar_t type to a char type, using wcsstr and wcsstr. With a raw string, I can use 'L' like so: wcsstr((uint16_t *)wchar_String, L"regularCharString") ...
3
by: _Christopher\(M2M\) | last post by:
Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) How do I get rid of "Warning C4307: '-' : integral constant overflow" from the following macro? #define...
15
by: Andreas Eibach | last post by:
.... but I have an unsigned long value in the printf. This warning came when I used gcc 4.x to compile. .... unsigned long offset = 0; .... Well OK, an "easy" way would be instead of printf...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.