Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operator Overload

Reply
Thread Tools

Operator Overload

 
 
Rama
Guest
Posts: n/a
 
      03-23-2005
wht is operator overloading, how you overload an operator

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-23-2005
Rama wrote:
> wht is operator overloading, how you overload an operator
>


You define a function using the special syntax for defining an operator:

[<return_value_type>] operator @ ( [<arguments>] )

The keyword 'operator' and the parentheses are necessary. The return
value type is not necessary for type conversion operators. Arguments
may be necessary depending on what operator you overload. The '@' sign
in the syntax designates the place where you put the operator sign. For
example, here is the single minus overload for struct T:

struct T {
T operator -() const;
};

What book are you reading that doesn't explain operator overloading?

V
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      03-23-2005
"Rama" writes:

> wht is operator overloading, how you overload an operator


Operator overloading is a means of calling a class member function when an
operator occurs in the source code. It allows nicer looking source code
when there is some reasonable similarity between the operator symbols and
some function to be performed on an object. One of the more appealing
examples is concatenation of a string, which could be codified as '+'.
Another nice one is that operators can be applied to complex numbers where
there is a one to one correlation between most of the arithmetic symbols for
real numbers and complex numbers. Beware, it is easy to be tempted to let
operator overloading run amok and use it where there is a dubious
relationship between the operators and the resulting action.

A simple example is as follows:
---------------------------
#include <iostream>

using namespace std;

class C
{
public:
C(int na) {n = na;}
bool operator>(C rhs); // rhs - right hand side
private:
int n;
};
//-----------------------
bool C:perator>(C rhs)
{
if(n > rhs.n)
return true;
else
return false;
}
//=====================
int main()
{
C c(1024);
C d(204;

bool x, y;
x = c>d;
y = d>c;

cout << x << ' ' << y << endl;
cin.get();
}



 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      03-23-2005
osmium wrote:

[ ... ]

> Operator overloading is a means of calling a class member function
> when an operator occurs in the source code.


Leave out "class member" in that sentence and it'll be more accurate.

--
Later,
Jerry.

The universe is a figment of its own imagination.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to handle the lack of operator overload ? Mr Smith Java 7 02-10-2005 08:37 AM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM
Q: Overload operator new/delete Jakob Bieling C++ 0 08-08-2003 12:20 PM
overload operator - Matthew Monopole C++ 1 08-08-2003 09:39 AM



Advertisments