Re: [Dev-C++] Operator overloading for built in types.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Abdelkader G. <go...@li...> - 2003-12-27 17:36:50
|
To have the code presented above compiling, you should try this:
--------------------------------------------------------------
#include <iostream>
#include <stdlib.h>
using namespace std;
//encapsulate the int type inside a
//structure
typedef struct
{
const int value;
} _int;
//the operator is now defined
//for the structure _int
bool operator==(const _int i,const _int j)
{
cout << "hello" <<endl;
return (i.value + j.value);
}
int main(int argc, char *argv[])
{
_int i = {1};
_int j = {-1};
cout << (i == j) <<endl;
return 0;
}
---------------------------------------------------
The output is:
hello
0
kind regards,
> hi there,
>
> Are you sure that you can overload operator for simple types that are
> not classes? You can emulate built in types operators for you
> defined 'classes', but I'm not sure that you can redefine operator for
> built in types and if this is allowed what a catastrophy it would be ,)
> imagine this (bogus) code:
>
> bool operator==(const int i,const int j)
> {
> return (bool) (i + j);
> }
> //this code change the semantics of ==
> // 4 == -4 -> true 4 == 4 ->false
>
> If you are allowed to change the definition of the == (int equals) this
> means that this operator is changed for 'all' int == operation, so can
> you imagine what happends if you include others source code that have
> used another semantics for the (int ==).
>
> So, I'm not sure that you can overload built in types but if you have
> more information I'm interested ,)
>
> Kind regards;
>
>> Ok here is my dilema.
>>
>> Scattered throught my code is the following line:
>>
>> if(!strcmp(var1,var2))
>> ....
>>
>> What would be the best way to change that to
>>
>> if( var1 == var2 )
>> ....
>>
>> So far here is some exaple code that illustrates my problem:
>> bool operator==(char *s1, char *s2)
>> {
>> if(!strcmp(s1,s2))
>> return true;
>> else
>> return false;
>> }
>>
>> int main()
>> {
>> char x[10] = "Hello";
>> char y[10] = "Hello";
>> if(x == y)
>> cout << "Equal" << endl;
>> else
>> cout << "Not equal" << endl;
>> cin.get();
>> return 0;
>> }
>>
>> Error:
>> C:/Documents and Settings/David/My Documents/scratch.cpp:9: `bool
>> operator==(char*, char*)' must have an argument of class or
>> enumerated type
>>
>> What does it mean?? I have done operator overloading with various
>> classes but I can't get it to work with char *, please help. There is
>> no one to make it a friend with so that is ruled out.
>>
>> =====
>> Signed
>> David Mcken
>>
>> Life Sucks
>> Live with it
>>
>> __________________________________
>> Do you Yahoo!?
>> New Yahoo! Photos - easier uploading and sharing.
>> http://photos.yahoo.com/
>>
>>
>> -------------------------------------------------------
>> This SF.net email is sponsored by: IBM Linux Tutorials.
>> Become an expert in LINUX or just sharpen your skills. Sign up for
>> IBM's Free Linux Tutorials. Learn everything from the bash shell to
>> sys admin. Click now!
>> http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
>> _______________________________________________
>> Dev-cpp-users mailing list
>> Dev...@li...
>> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: IBM Linux Tutorials.
> Become an expert in LINUX or just sharpen your skills. Sign up for
> IBM's Free Linux Tutorials. Learn everything from the bash shell to
> sys admin. Click now!
> http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|