473,404 Members | 2,213 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,404 software developers and data experts.

operator+ cant be constant

Hi all,

Jesse Liberty writes:

"Note that the String class provides the operator+. The designer of
the Employee class has
blocked access to the operator+ being called on Employee objects by
declaring that all the string
accessors, such as GetFirstName(), return a constant reference.
Because operator+ is not (and
can't be) a const function (it changes the object it is called on),
attempting to write the following will
cause a compile-time error:

String buffer = Edie.GetFirstName() + Edie.GetLastName();"

(the String class he refers to is a custom String class coded for
demonstration purposes)

I agree that the line will cause a compile time error because operator
+ isn't defined as a const method:

constString.operator+(anotherConstString) <- will not compile

What I don't understand is his statement saying that operator+ can't
be a const function.. how does operator+ change the object that it's
being called on? AFAIK, operator+ should return a new object which is
a concatenation of the two, and shouldn't effect either operand.

Taras
Jun 27 '08 #1
6 1666
Taras_96 wrote:
Hi all,

Jesse Liberty writes:

"Note that the String class provides the operator+. The designer of
the Employee class has
blocked access to the operator+ being called on Employee objects by
declaring that all the string
accessors, such as GetFirstName(), return a constant reference.
Because operator+ is not (and
can't be) a const function (it changes the object it is called on),
attempting to write the following will
cause a compile-time error:

String buffer = Edie.GetFirstName() + Edie.GetLastName();"

(the String class he refers to is a custom String class coded for
demonstration purposes)

I agree that the line will cause a compile time error because operator
+ isn't defined as a const method:

constString.operator+(anotherConstString) <- will not compile

What I don't understand is his statement saying that operator+ can't
be a const function.. how does operator+ change the object that it's
being called on? AFAIK, operator+ should return a new object which is
a concatenation of the two, and shouldn't effect either operand.
That depends how the operator is declared. The binary operator + can be
declared as either a member function, or a free function. If it is a
member function, the string object is the left hand side of the
operation that is the right hand side is added to the object the
operator is called on.

If the operator is a free function, a new object is returned.

A simple example:

struct X
{
int n;

X( int n = 0 ) : n(n) {}

X& operator+( const X& other )
{
n += other.n;
return *this;
}
};

X operator+( const X& lhs, const X& rhs )
{
return lhs.n+rhs.n;
}

--
Ian Collins.
Jun 27 '08 #2
Ian Collins wrote:
Taras_96 wrote:
>Hi all,

Jesse Liberty writes:

"Note that the String class provides the operator+. The designer of
the Employee class has
blocked access to the operator+ being called on Employee objects by
declaring that all the string
accessors, such as GetFirstName(), return a constant reference.
Because operator+ is not (and
can't be) a const function (it changes the object it is called on),
attempting to write the following will
cause a compile-time error:

String buffer = Edie.GetFirstName() + Edie.GetLastName();"

(the String class he refers to is a custom String class coded for
demonstration purposes)

I agree that the line will cause a compile time error because operator
+ isn't defined as a const method:

constString.operator+(anotherConstString) <- will not compile

What I don't understand is his statement saying that operator+ can't
be a const function.. how does operator+ change the object that it's
being called on? AFAIK, operator+ should return a new object which is
a concatenation of the two, and shouldn't effect either operand.
That depends how the operator is declared. The binary operator + can be
declared as either a member function, or a free function. If it is a
member function, the string object is the left hand side of the
operation that is the right hand side is added to the object the
operator is called on.

If the operator is a free function, a new object is returned.

A simple example:

struct X
{
int n;

X( int n = 0 ) : n(n) {}

X& operator+( const X& other )
{
n += other.n;
return *this;
}
};

X operator+( const X& lhs, const X& rhs )
{
return lhs.n+rhs.n;
}
Could it be that you are confusing operator+ and operator+= ?

I dont' see any reason why a member operator+ should not be const.
Best

Kai-Uwe Bux

Jun 27 '08 #3
Kai-Uwe Bux wrote:
>
Could it be that you are confusing operator+ and operator+= ?

I dont' see any reason why a member operator+ should not be const.
I did, good spot.

--
Ian Collins.
Jun 27 '08 #4
On Thu, 24 Apr 2008 23:54:12 -0700, Taras_96 wrote:
I agree that the line will cause a compile time error because operator +
isn't defined as a const method:

constString.operator+(anotherConstString) <- will not compile

What I don't understand is his statement saying that operator+ can't be
a const function.. how does operator+ change the object that it's being
called on? AFAIK, operator+ should return a new object which is a
concatenation of the two, and shouldn't effect either operand.
From looking at the source in his book (Teach Yourself C++ in 21 days,
right??) the implementation of operator+ does not change the object at
all so it can (and should) be const.

Btw. as his proposed 'fix' is to have Employee::GetFirstName() to return
a non const reference to a member variable and seeing some of the example
code in his book I wont trust the things he writes in his book too
much... So, my guess: you are right, he is wrong.

Ralph.
Jun 27 '08 #5
Ian Collins <ia******@hotmail.comwrote in news:67de8eF2ojm2dU1
@mid.individual.net:
Taras_96 wrote:
>Hi all,

Jesse Liberty writes:

"Note that the String class provides the operator+. The designer of
the Employee class has
blocked access to the operator+ being called on Employee objects by
declaring that all the string
accessors, such as GetFirstName(), return a constant reference.
Because operator+ is not (and
can't be) a const function (it changes the object it is called on),
attempting to write the following will
cause a compile-time error:

String buffer = Edie.GetFirstName() + Edie.GetLastName();"

(the String class he refers to is a custom String class coded for
demonstration purposes)

I agree that the line will cause a compile time error because operator
+ isn't defined as a const method:

constString.operator+(anotherConstString) <- will not compile

What I don't understand is his statement saying that operator+ can't
be a const function.. how does operator+ change the object that it's
being called on? AFAIK, operator+ should return a new object which is
a concatenation of the two, and shouldn't effect either operand.
That depends how the operator is declared. The binary operator + can be
declared as either a member function, or a free function. If it is a
member function, the string object is the left hand side of the
operation that is the right hand side is added to the object the
operator is called on.

If the operator is a free function, a new object is returned.

A simple example:

struct X
{
int n;

X( int n = 0 ) : n(n) {}

X& operator+( const X& other )
{
n += other.n;
return *this;
}
};
And this is evil. operator+() (assuming "normal" semantics for operator+)
has no business modifying n. This isn't operator+=() !

I'd argue that operator+() _should_ be declared const.

Jun 27 '08 #6
On Apr 25, 6:43 pm, Ralph <r...@et10.orgwrote:
On Thu, 24 Apr 2008 23:54:12 -0700, Taras_96 wrote:
I agree that the line will cause a compile time error because operator +
isn't defined as a const method:
constString.operator+(anotherConstString) <- will not compile
What I don't understand is his statement saying that operator+ can't be
a const function.. how does operator+ change the object that it's being
called on? AFAIK, operator+ should return a new object which is a
concatenation of the two, and shouldn't effect either operand.

From looking at the source in his book (Teach Yourself C++ in 21 days,
right??) the implementation of operator+ does not change the object at
all so it can (and should) be const.

Btw. as his proposed 'fix' is to have Employee::GetFirstName() to return
a non const reference to a member variable and seeing some of the example
code in his book I wont trust the things he writes in his book too
much... So, my guess: you are right, he is wrong.

Ralph.
:) Thanks Ralph
Jun 27 '08 #7

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

Similar topics

5
by: ··········· sylvain | last post by:
hello, what does the operator |= mean? I saw it in a script : $output = "png"; $style = BCS_ALIGN_CENTER; $style |= ($output == "png" ) ? BCS_IMAGE_PNG : 0;
5
by: Roger Leigh | last post by:
I've written a simple container template class to contain a single value. This emits a signal when the value is changed (it's used as a notifier of changes), and listeners can connect to its...
7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
5
by: James Angi | last post by:
I have a question on operator overloading that I can't find an answer to in several guides (even google has failed me). I'm currently making my way through several C++ guides, trying to become...
23
by: Randy | last post by:
Since these operators can't be member functions, and since friend functions can't be declared virtual, how do I make my inserters and extractors polymorphic? --Randy Yates
10
by: olson_ord | last post by:
Hi, I am not exactly new to C++, but I have never done operator overloading before. I have some old code that tries to implement a Shift Register - but I cannot seem to get it to work. Here's a...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
7
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
5
by: Chris Forone | last post by:
hello group, why i cant make the InputIterator of the following func-temp const? template<typename InputIterator, typename OutputIterator> OutputIterator Types::Copy(InputIterator source,...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.