473,388 Members | 1,496 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,388 software developers and data experts.

Operator Overload

wht is operator overloading, how you overload an operator

Jul 23 '05 #1
3 1762
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
Jul 23 '05 #2
"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::operator>(C rhs)
{
if(n > rhs.n)
return true;
else
return false;
}
//=====================
int main()
{
C c(1024);
C d(2048);

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

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

Jul 23 '05 #3
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.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
7
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
7
by: Genival Carvalho | last post by:
Hello friends... Inside my class how do to use explicit using Overloaded operator or use default ? Ex: Dim x as Integer = A + B ' Use default + operator Dim OV as integer = X + Z ' I will...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...

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.