Operator Overloading

Discussion in 'C++' started by James Angi, Feb 23, 2005.

  1. James Angi

    James Angi Guest

    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
    James Angi, Feb 23, 2005
    #1
    1. Advertising

  2. * 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?
    Alf P. Steinbach, Feb 23, 2005
    #2
    1. Advertising

  3. James Angi

    James Angi Guest

    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" <> wrote in message
    news:...
    >* 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?
    James Angi, Feb 23, 2005
    #3
  4. * 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?
    Alf P. Steinbach, Feb 23, 2005
    #4
  5. James Angi

    James Angi Guest

    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" <> wrote in message
    news:...
    >* 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?
    James Angi, Feb 23, 2005
    #5
  6. James Angi

    Howard Guest

    "James Angi" <> 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
    Howard, Feb 23, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    407
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    707
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    708
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    538
  5. Replies:
    2
    Views:
    292
Loading...

Share This Page