473,783 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetFirstNa me() + Edie.GetLastNam e();"

(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.ope rator+(anotherC onstString) <- 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 1688
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.GetFirstNa me() + Edie.GetLastNam e();"

(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.ope rator+(anotherC onstString) <- 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.GetFirstNa me() + Edie.GetLastNam e();"

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

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

constString.op erator+(another ConstString) <- 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.ope rator+(anotherC onstString) <- 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::GetFi rstName() 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******@hotma il.comwrote in news:67de8eF2oj m2dU1
@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.GetFirstNa me() + Edie.GetLastNam e();"

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

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

constString.op erator+(another ConstString) <- 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.orgw rote:
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.ope rator+(anotherC onstString) <- 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::GetFi rstName() 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
2268
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
2124
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 changed signal. i.e. field<int> i(2); i = 4; // field<int>::m_value = 4; changed signal is emitted. Currently, the contained value may be accessed via get_value() and set_value() methods, and for ease of use, operator= and some type conversions...
7
2441
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 double, I have to define a global operator+ (or -*/) and special function classes. --- source begins here ---
5
491
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 familiar with the language's features. The book I'm in the middle of right now says an operator+ function cannot be const. For example, it seems to imply: class String { public:
23
2528
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
3150
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 simpler version of it. -------------------- main.cpp--------------------------- # include <iostream>
3
3464
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 memory allocation? like complex<int> * c1,*c2,*c3; i still want to do something like c3 = c1+c2 ; rether than *c3 = *c1+*c2;
7
230
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
2102
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, OutputIterator first, OutputIterator last) { while (first != last) *first++ = *source++;
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.