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. 7 2161
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 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
"mlimber" <ml*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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
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.
Howard wrote: "mlimber" <ml*****@gmail. com> wrote in message news:11******** **************@ z14g2000cwz.goo glegroups.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::MyFloa t(
double d )
is indeed missing.
HTH,
Michiel Salters
<Mi************ *@tomtom.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. . Howard wrote: "mlimber" <ml*****@gmail. com> wrote in message news:11******** **************@ z14g2000cwz.goo glegroups.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::MyFloa t( 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
"Old Wolf" <ol*****@inspir e.net.nz> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 variable $noFile,
it is print "1" instead of "true". Why does it do this?
The loop is failing to run a second time, for reasons I can not
fathom. debugNotes tells me that the count of $dirArray is 10, and the
file hasn't been found yet, so I...
|
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
--
Christian Staudenmayer
|
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 compiles
fine.
What is the warning shown ?
|
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 last_values as well as assign the
value of a pointer to the assigned space. Which I think I'm doing by
using:
|
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)?
Does "x**0.5" use the same algorithm as "sqrt(x)", or does it use some
other (perhaps less efficient) algorithm based on logarithms?
Thanks,
Russ
| |
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 same section
of code, but I can run the code a few times, then it appears. I can't
reproduce it when I want to, and I have no idea what is causing it.
Any ideas ? Thanks
By the way the code is some record manipulation of 2 recordsets.
|
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")
BUT... say I have:
|
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 d_ReturnMax(I_iTypeSize)\
( (int64(1)<<((I_iTypeSize*8)-1))-1)
There NO warnings when done as a function:
int64 d_ReturnMax(int64 I_iTypeSize)
{ return (1<<((I_iTypeSize*8)-1))-1;}
|
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 ("eof found at offset %08x", offset);
to do a type cast like
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |