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

C++: operator overloading

A
Hi,

I understand that operator loading is used to support user-defined types.
One of the requirements is that one of the arguments in an operator
overloaded function must be of a user defined type. My question is, how do
you know if this argument refers to the left or right operand in an
expression. I assume in a single argument it refers to the right operand.

Any help appreciated,
Regards
A
Jul 19 '05 #1
3 13538
Hello A,

"A" <A@iprimus.com.au> writes:
I understand that operator loading is used to support user-defined types.
One of the requirements is that one of the arguments in an operator
overloaded function must be of a user defined type. My question is, how do
you know if this argument refers to the left or right operand in an
expression. I assume in a single argument it refers to the right operand.


You may choose which argument is user-defined. If you write your own matrix
class and want to be able to add integers to it (perhaps it will add so
many times the unit matrix to your original matrix) you should define

matrix operator+(const matrix&,const int)

and, because hopefully your addition is commutative also

matrix operator+(const int,const matrix&)

The point is that these two + operators may make sense but that I would
really started hating a library writer that redefines the additon of two
integers. Fortunately the language forbids this (Although, you may turn
int into a macro that expands to my_int and then define an operator+ for
it, of course...)

Bye,
Chris Dams
Jul 19 '05 #2
A
Hi,

I understand that operator loading is used to support user-defined types.
One of the requirements is that one of the arguments in an operator
overloaded function must be of a user defined type. My question is, how do
you know if this argument refers to the left or right operand in an
expression. I assume in a single argument it refers to the right operand.

Any help appreciated,
Regards
A


consider:

When overloading +, you can either declare it as a member function of its
class or as a friend function. For example:
class Date
{
public:
Date operator +(const Date& other); //member function
};
class Year
{
friend Year operator+ (const Year y1, const Year y2); //friend
};
Year operator+ (const Year y1, const Year y2);
The friend version is preferred because it reflects symmetry between the two
operands. Since built-in + does not modify any of its operands, the
parameters of the overloaded + are declared const. Finally, overloaded +
should return the result of its operation by value, not by reference.
Question: why not use a member function that takes two arguments like the
friend function so that it it too has the property of symmetry?

REgards
A
Jul 19 '05 #3
On Tue, 11 Nov 2003 19:17:53 +1030, A <A@iprimus.com.au> wrote:
Hi,

I understand that operator loading is used to support user-defined types.
One of the requirements is that one of the arguments in an operator
overloaded function must be of a user defined type. My question is, how
do
you know if this argument refers to the left or right operand in an
expression. I assume in a single argument it refers to the right operand.
No, it refers to the hidden this argument( left in the expression).
Implicit conversions can be aplied to the right argument.
In the friend version implicit conversions can be applied to match
both of the arguments of defined operator.
One of the arguments of the friend version must be of class/struct type,
that is you cannot overload operator for builtin types.

Here is an example:

struct intm {
int dat;
intm(int x) : dat(x) {}
intm operator +(const intm& y) { return intm(dat+y.dat); }
};
struct intf : intm {
intf(int x) : intm(x) {}
intf(const intm& m) : intm(m) {}
friend intf operator +(const intf& l,const intf& r) { return
intf(l.dat+r.dat); }
};

int main() {
intm mx=intm(5)+6;
// intm my=6+intm(5); will not work , member version
intf fx=intf(5)+6;
intf fy=6+intf(5);
intf fz=7+mx; // this works because of intf operator + , and implicit
conversions
}


Any help appreciated,
Regards
A


--
grzegorz
Jul 19 '05 #4

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

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
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,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
tracyyun
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...

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.