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

how to write an operator

which is preferable and WHY?

class Complex {
....
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs);

OR either one but a member operator instead of a friend.

Thanx,
Martin
Jul 22 '05 #1
9 1558
Martin Vorbrodt wrote:
which is preferable and WHY?

class Complex {
...
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs); ^
That const adds no value, and there are those who claim it interferes with
certain type-specific operations.

If you meant 'Complex const &', don't do that either, because functions
generally should not return value-types by reference.
OR either one but a member operator instead of a friend.


A member operator might not balance properly:

5 + Complex(8, 2);

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2
>That const adds no value

Well, if you believe what some people say about operator overloading and "do as
the ints do" then it does make it behave more like a function that returns a
built-in type by value.

int a,b,c;
(a+b) = c; //This should not compile

Complex a,b,c;
(a+b) = c; //This will compile if return value is not const.

For reasons I have never grasped a user-defined type returned by value is not
an lvalue yet it can be the target of modifying operations. To make the
overloaded operator behave in a way analagous to the built-in operators you
must make the return value const.

Does this have value?

I don't know.
Jul 22 '05 #3
"DaKoadMunky" <da*********@aol.com> wrote in message
Phlip
friend const Complex operator + (const Complex& lhs, const Complex&
rhs);
That const adds no value

Well, if you believe what some people say about operator overloading and "do as the ints do" then it does make it behave more like a function that returns a built-in type by value.

int a,b,c;
(a+b) = c; //This should not compile

Complex a,b,c;
(a+b) = c; //This will compile if return value is not const.

For reasons I have never grasped a user-defined type returned by value is not an lvalue yet it can be the target of modifying operations. To make the
overloaded operator behave in a way analagous to the built-in operators you must make the return value const.

Does this have value?

I don't know.


Yes, it prevents accidentally modifying a newly returned object, so might
catch code which compiles but may do the wrong thing.

a++ = b;
Jul 22 '05 #4

"Martin Vorbrodt" <mv*******@poczta.onet.pl> skrev i en meddelelse
news:cg**********@news.onet.pl...
which is preferable and WHY?

class Complex {
...
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs);

OR either one but a member operator instead of a friend.

Thanx,
Martin

You should prefer

class Complex
{
Complex& operator+=(Complex const& rhs);
......

then you can have:
Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
rhs; }
Jul 22 '05 #5
So how about +=, etc operators.
I assume they return a reference, not a constant referance,
since this code is legal and works just fine:

int a = 1, b = 2, c = 3;
a += b += c;
(a += b) += c;
a += (b += c);
"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m06.aol.com...
That const adds no value
Well, if you believe what some people say about operator overloading and

"do as the ints do" then it does make it behave more like a function that returns a built-in type by value.

int a,b,c;
(a+b) = c; //This should not compile

Complex a,b,c;
(a+b) = c; //This will compile if return value is not const.

For reasons I have never grasped a user-defined type returned by value is not an lvalue yet it can be the target of modifying operations. To make the
overloaded operator behave in a way analagous to the built-in operators you must make the return value const.

Does this have value?

I don't know.

Jul 22 '05 #6
Why would i want to do that? That would modify LHS variable. I don't want
that.
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:pg********************@news000.worldonline.dk ...

"Martin Vorbrodt" <mv*******@poczta.onet.pl> skrev i en meddelelse
news:cg**********@news.onet.pl...
which is preferable and WHY?

class Complex {
...
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs);
OR either one but a member operator instead of a friend.

Thanx,
Martin

You should prefer

class Complex
{
Complex& operator+=(Complex const& rhs);
.....

then you can have:
Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
rhs; }

Jul 22 '05 #7
"Martin Vorbrodt" <mv*******@poczta.onet.pl> wrote in message news:cgsrv4
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
You should prefer

class Complex
{
Complex& operator+=(Complex const& rhs);
.....

then you can have:
Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
rhs; }


That's assuming we want both operators, which is a reasonable assumption.
Why would i want to do that? That would modify LHS variable. I don't want
that.


It's fine. Look closely at the function arguments. The function receives
lhs by value, so it's a copy. However, it should be declared not const.
This should suffice.

Complex operator +(Complex lhs,Complex const& rhs) { return lhs += rhs; }

You can optionally return a const Complex, as indicated in the other
sub-thread to prevent accidental assignment to the returned unnamed
temporary.

const Complex operator +(Complex lhs,Complex const& rhs) { return lhs +=
rhs; }
Jul 22 '05 #8
> "DaKoadMunky" <da*********@aol.com> wrote in message
"Martin Vorbrodt" <mv*******@poczta.onet.pl> wrote in message news:cgsrtm$nd9

We prefer if you reply to posts in place, that is include the quoted text
then your comments to that quoted text, then more quoted text. This makes
it easier for people to read. At work of course, I usually just hit the
reply button as it's faster.
int a,b,c;
(a+b) = c; //This should not compile
So how about +=, etc operators.
I assume they return a reference, not a constant referance,
since this code is legal and works just fine:

int a = 1, b = 2, c = 3;
a += b += c;
(a += b) += c;
a += (b += c);


In return a const object, we're talking about returned a value. Of course,
returning a reference to an object is another story. We have to return a
non-const or const reference as appropriate to the design. As for returning
an object by value, as with operator++ operator+, it doesn't really matter
whether we return the object as const or not, but returning const is a
little safer and could catch strange bugs.
Jul 22 '05 #9

"Martin Vorbrodt" <mv*******@poczta.onet.pl> skrev i en meddelelse
news:cg**********@news.onet.pl...
Why would i want to do that? That would modify LHS variable. I don't want
that.
Because you almost certainly wants the operator+=. For users of your class
it will be confusing if they can write a = a + b but not a += b.

As you can see from my code, this makes operator+ very easy to write - and
it does not have to be a friend function.

Also i must add that i find your reply quite confusing. Do not toppost.

Kind regards
Peter


"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:pg********************@news000.worldonline.dk ...

"Martin Vorbrodt" <mv*******@poczta.onet.pl> skrev i en meddelelse
news:cg**********@news.onet.pl...
which is preferable and WHY?

class Complex {
...
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs);
OR either one but a member operator instead of a friend.

Thanx,
Martin

You should prefer

class Complex
{
Complex& operator+=(Complex const& rhs);
.....

then you can have:
Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
rhs; }


Jul 22 '05 #10

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

Similar topics

8
by: daniel.w.gelder | last post by:
Hello, I have been trying to write a functor template for a week now and I'm just having tons of trouble because I don't understand an issue that I guess is pretty basic to this task. ...
2
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically...
3
by: Jim Langston | last post by:
I have a CSkill class which is rather complex as it is recursive. That is: class CSkill { public: CSkill( std::string Name, float Value ): Name_( Name ), Value_( Value ) {}; void Update(...
8
by: av | last post by:
i have my little string class, now i want do define something like this but for doing it i have to write sstream& operator+(char* a, char* b){} but compiler say something like...
3
by: key9 | last post by:
Hi all I am confuse of how to override operate "<<" Sample , I've got 2 class class Terminal { public:
3
by: cmk128 | last post by:
Hi class A { }; class B { public: static void* operator new(size_t sz, A &a) {
8
by: mohammaditraders | last post by:
#include <iostream.h> #include <stdlib.h> #include <conio.h> #include <string.h> class Matrix { private : int numRows, numCols ; int elements ;
11
by: jacob navia | last post by:
Hi Suppose that I want to create an array of read only items I overload the operator. How can I detect if I am being called within a read context foo = Array; or within a write context...
9
by: kk_oop | last post by:
Consider class X and Y where: class X { double d; int i; Y myY; }; class Y
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.