Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operator Overloading

Reply
Thread Tools

Operator Overloading

 
 
James Angi
Guest
Posts: n/a
 
      02-23-2005
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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-23-2005
* 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).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
James Angi
Guest
Posts: n/a
 
      02-23-2005
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
"Alf P. Steinbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>* 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).
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?



 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-23-2005
* James Angi:
>
> Title: Sams Teach Yourself C++ in 21 days
> Authors: Jesse Liberty and Bradley Jones
> ISBN: 0-672-32711-2
>
> * Alf P. Steinbach:
> >
> > (and burn the book).


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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
James Angi
Guest
Posts: n/a
 
      02-23-2005
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

"Alf P. Steinbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>* James Angi:
>>
>> Title: Sams Teach Yourself C++ in 21 days
>> Authors: Jesse Liberty and Bradley Jones
>> ISBN: 0-672-32711-2
>>
>> * Alf P. Steinbach:
>> >
>> > (and burn the book).

>
> Yes, definitely, why didn't you say it was _that_, uh, unspeakable?
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?



 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      02-23-2005

"James Angi" <(E-Mail Removed)> wrote in message
news:eQVSd.27721$Cs.16635@okepread02...
> 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:

>>
>> --
>> A: Because it messes up the order in which people normally read text.
>> Q: Why is it such a bad thing?
>> A: Top-posting.
>> Q: What is the most annoying thing on usenet and in e-mail?

>
>


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments