Home > Archive > VC Language > November 2005 > Operator Overloading
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Operator Overloading
|
|
| Jonathan 2005-11-24, 4:01 am |
| I know that you can overload objects, but can the standard data types
(integers, short, long, floats, etc.) have their = operator overloaded?
I am developing a client server data application in C++ and I don't want to
poll to see if the data has changed. I was thinking that overlaoding the =
operator would indicate when there has been a change.
Ofcourse I could make my own classes, but I have tens of thousands of lines
of code that use the standard data types. Bascially, I just want to know what
data has been changed and update it to the server without changing my
exisitng code if possible.
Is it possible to have an event triggered when a variable is changed?
Overloading the = operator would be the same idea. Just wondering if there is
something in MFC or the C++ language that would tell me when the value of a
variable changes. The catch is that it has to be efficent too since my app
has 750,000 variables.
Are there any low level hooks into the OS that would help?
I know its asking a lot, but does anyone have any ideas? Thanks for the help.
| |
| Igor Tandetnik 2005-11-24, 4:01 am |
| "Jonathan" <Jonathan@discussions.microsoft.com> wrote in message
news:7E5BADF2-5F53-4D56-8BF6-BBA3D2730D67@microsoft.com...
>I know that you can overload objects, but can the standard data types
> (integers, short, long, floats, etc.) have their = operator
> overloaded?
No.
> Are there any low level hooks into the OS that would help?
No.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| Abdo Haji-Ali 2005-11-24, 7:59 am |
| Well, there is a way… A little complicated but it works; just declare a new
class as your basic type, and then define it as the actual basic type…
Something like this:
class CMyInt
{
public:
int operator=(int iBasic)
{
iMain = iBasic;
return iMain;
}
operator int() const
{
return iMain;
}
int iMain;
};
#define int CMyInt
main()
{
int i;
i = 5;
return i;
}
Of course you'll have to drop the "int i=5;" statements, and use a
constructor instead; also you'll have to define all the basic operators for
your class
Abdo Haji-Ali
Programmer
In|Framez
"Jonathan" <Jonathan@discussions.microsoft.com> wrote in message
news:7E5BADF2-5F53-4D56-8BF6-BBA3D2730D67@microsoft.com...
>I know that you can overload objects, but can the standard data types
> (integers, short, long, floats, etc.) have their = operator overloaded?
>
> I am developing a client server data application in C++ and I don't want
> to
> poll to see if the data has changed. I was thinking that overlaoding the =
> operator would indicate when there has been a change.
>
> Ofcourse I could make my own classes, but I have tens of thousands of
> lines
> of code that use the standard data types. Bascially, I just want to know
> what
> data has been changed and update it to the server without changing my
> exisitng code if possible.
>
> Is it possible to have an event triggered when a variable is changed?
> Overloading the = operator would be the same idea. Just wondering if there
> is
> something in MFC or the C++ language that would tell me when the value of
> a
> variable changes. The catch is that it has to be efficent too since my app
> has 750,000 variables.
>
> Are there any low level hooks into the OS that would help?
>
> I know its asking a lot, but does anyone have any ideas? Thanks for the
> help.
| |
| John Carson 2005-11-24, 7:01 pm |
| "Abdo Haji-Ali" <ahali@inframez.org_use_com_instead> wrote in message
news:O8yOH3O8FHA.3044@TK2MSFTNGP10.phx.gbl
> Well, there is a way… A little complicated but it works; just declare
> a new class as your basic type, and then define it as the actual
> basic type… Something like this:
>
>
>
> class CMyInt
>
> {
> public:
> int operator=(int iBasic)
> {
> iMain = iBasic;
> return iMain;
> }
>
> operator int() const
> {
> return iMain;
> }
>
> int iMain;
> };
>
>
> #define int CMyInt
>
>
> main()
> {
> int i;
> i = 5;
> return i;
> }
>
>
> Of course you'll have to drop the "int i=5;" statements, and use a
> constructor instead; also you'll have to define all the basic
> operators for your class
You don't need to drop "int i=5;" statements. Just supply the following
conversion constructor:
CMyInt(int rhs) : iMain(rhs)
{}
I note that you have omitted the return type from main. This is non-standard
and many compilers won't allow it. If you provide an int return type,
however, then the macro turns this into a CMyInt and it fails to compile.
You can get around this with
typedef int plainint;
#define int CMyInt
plainint main()
{
int i;
i = 5;
return 0;
}
The need for this workaround, however, illustrates a more general problem.
The conversion operator doesn't make CMyInt a perfect substitute for int.
Consider:
#include <iostream>
using namespace std;
class CMyInt
{
public:
CMyInt(int rhs) : iMain(rhs)
{}
int operator=(int iBasic)
{
iMain = iBasic;
return iMain;
}
operator int() const
{
return iMain;
}
private:
int iMain;
};
struct Test
{
Test(int x) : number(x)
{}
int number;
};
void foo(const Test & t)
{}
typedef int plainint;
#define int CMyInt
plainint main()
{
int x(5);
foo(x);
}
This won't compile, whereas it will if you use native ints (the reason it
fails with CMyInt is that foo(x) then requires two user defined
conversions --- CMyInt to int and int to Test --- and the compiler will only
do one). In this case, you can avoid the problem by moving the typedef and
#define before the declaration of the Test struct, but the analogous
strategy is likely to be disastrous when used with third party libraries
(including the Windows API) which have been designed using a native int.
--
John Carson
|
|
|
|
|