Operator Overloading

J

James Angi

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:
String operator+(const String&) const;
};

is invalid... yet it compiles, works fine, and while I realize that this
does not mean it is 'correct', I can't think of a reason why an operator+
method could not be declared const.

Thanks,
James
 
A

Alf P. Steinbach

* James Angi:
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:
String operator+(const String&) const;
};

is invalid... yet it compiles, works fine, and while I realize that this
does not mean it is 'correct', I can't think of a reason why an operator+
method could not be declared const.

operator+ should be const, and of course can be const.

operator+= should not be const.

It's possible that the standard says something about operator+= not being
const, but I doubt it (if it ever becomes an issue, i.e. you're considering
a const operator+=, then a design error is indicated).

I suggest first checking out the context and precise wording of the book's
statement(s).

If it then still says operator+ cannot be const, then do please post the title
of the book here so that people can avoid it (and burn the book).
 
J

James Angi

Perhaps I'm not reading it right, below is the book info and the text to
which I refered to. Earlier in the chapter a example String class is
provided, and sure enough the operator+ method is non-const. The text is
misleading though, because that method does not change the object it is
being called on. Instead of modifying the example Employee class as
suggested, I simply made operator+ const, and it works as expected. Just a
book typo/mistake or am I not grasping this yet?

Title: Sams Teach Yourself C++ in 21 days
Authors: Jesse Liberty and Bradley Jones
ISBN: 0-672-32711-2

Chapter 11: Advanced Inheritance
Note that the String class provides an overloaded plus operator: 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
a 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 causes a compile-time error:

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

GetFirstName() returns a constant String, and you can't call operator+ on a
constant object.
To fix this, overload GetFirstName() to be non-const:
const String & GetFirstName() { return itsFirstName; }
String & GetFirstName() { return itsFirstName; }


Controlling Acces
 
A

Alf P. Steinbach

* James Angi:
Title: Sams Teach Yourself C++ in 21 days
Authors: Jesse Liberty and Bradley Jones
ISBN: 0-672-32711-2

* Alf P. Steinbach:

Yes, definitely, why didn't you say it was _that_, uh, unspeakable?
 
J

James Angi

What can I say... it was on clearance. It's had some good material though.
Thanks for your help, that one little paragraph was causing me great
confusion.
~James
 
H

Howard

James Angi said:
What can I say... it was on clearance. It's had some good material
though. Thanks for your help, that one little paragraph was causing me
great confusion.
~James
James,
I think you've also been missing the point of Alf's P.S, below:

You keep posting your responses at the top, which is called top-posting.
Please respond in-line (as I have above), or at the bottom, as I am here,
and trim what's no longer needed (as I have).

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top