Operator overloading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • victor75040@yahoo.com

    #1

    Operator overloading

    Before you all start flaming me, I am not a student and this is not
    for any homework. Just someone learing c++ on their own.
    I am now up to the chapter in my book that describes operator
    overloading.

    I just cannot find any explanation that clearly point out what the
    parts of the statement refer to. For example the book says:

    comp operator+(comp b)

    is the same as

    a . operator+ (b)

    I dont understand how is it a.operator, I wish there wasa graphical
    explanation that shows what each component represents. I have searched
    many sites, and found a lot of sites with Operator overloading
    articles, but NONE give a clear explanation to me, maybe I am just not
    getting it. Anyone care to share any sites that can help me out or
    give me a shot with their explanation. And by the way, if I have
    posted to the wrong group please forgive me, I have seen so many
    people get chewed out for asking a question here that I am sometimes
    hesitant to post. I know some questions rightfully dont belong here, I
    hope I am in the right place. For all those that take the time to
    answer my question, I thank you all in advance for your time and
    attention.

    victor75040@yah oo.com
    if you want to directly email me.

  • John Harrison

    #2
    Re: Operator overloading


    <victor75040@ya hoo.com> wrote in message
    news:n3g3jvcqn2 8itqtbh361kuuvp dt088u70p@4ax.c om...[color=blue]
    > Before you all start flaming me, I am not a student and this is not
    > for any homework. Just someone learing c++ on their own.
    > I am now up to the chapter in my book that describes operator
    > overloading.
    >
    > I just cannot find any explanation that clearly point out what the
    > parts of the statement refer to. For example the book says:
    >
    > comp operator+(comp b)
    >
    > is the same as
    >
    > a . operator+ (b)[/color]

    They aren't the same. The first one looks like a member declaration, the
    second one is a member function call.
    [color=blue]
    >
    > I dont understand how is it a.operator, I wish there wasa graphical
    > explanation that shows what each component represents.[/color]

    Component? When you overload operators, you have to write a function and you
    have to give that function a names. If you overload + then the name is of
    the function is operator+.

    a + b

    is short for either

    a.operator+(b)

    or

    operator+(a, b)
    [color=blue]
    > I have searched
    > many sites, and found a lot of sites with Operator overloading
    > articles, but NONE give a clear explanation to me, maybe I am just not
    > getting it. Anyone care to share any sites that can help me out or
    > give me a shot with their explanation. And by the way, if I have
    > posted to the wrong group please forgive me, I have seen so many
    > people get chewed out for asking a question here that I am sometimes
    > hesitant to post. I know some questions rightfully dont belong here, I
    > hope I am in the right place. For all those that take the time to
    > answer my question, I thank you all in advance for your time and
    > attention.[/color]

    Its the right group, your question is entirely about the C++ language.

    Maybe what you are getting at is this. Given the expression

    a + b

    where a and b are objects (of type A say) the compiler can interpret that
    two ways. It can call a global function, like this

    operator+(a, b)

    That's just like a regular function call, but it has a strange name. Or it
    can call a member function like this

    a.operator+(b)

    again this is just like any other member function call except for the
    strange name.

    So which one happens, which way does the compiler interpret it? It all
    depends one what you have declared for operator+. If you have written
    something like this

    A operator+(A a, A b)
    {
    ...
    }

    then the compiler is going to treat a + b as a global function call. On the
    other hand if you have written something like this

    class A
    {
    public:
    A operator+(A b)
    {
    ...
    }
    };

    then the compiler is going to treat a + b as a member function call.

    Thats all there is to it.

    HTH
    john


    Comment

    • David White

      #3
      Re: Operator overloading

      <victor75040@ya hoo.com> wrote in message
      news:n3g3jvcqn2 8itqtbh361kuuvp dt088u70p@4ax.c om...[color=blue]
      > Before you all start flaming me, I am not a student and this is not
      > for any homework.[/color]

      No need to be so defensive. Your question is a reasonable one.
      [color=blue]
      > Just someone learing c++ on their own.
      > I am now up to the chapter in my book that describes operator
      > overloading.
      >
      > I just cannot find any explanation that clearly point out what the
      > parts of the statement refer to. For example the book says:
      >
      > comp operator+(comp b)[/color]

      operator+ requires two arguments (because it adds two values together), so
      this looks like a declaration from the definition of class comp.

      class comp
      {
      public:
      // members
      comp operator+(comp b);
      // more members
      };

      The object you call the function for is the first argument, and b is the
      second argument.
      [color=blue]
      > is the same as
      >
      > a . operator+ (b)[/color]

      This is where you call the operator declared above:
      comp a(1); // assume constructor with numerical arg
      comp b(2);
      comp c = a.operator+(b); // 1 + 2

      This computes a + b (1 + 2) and stores the result in c.
      [color=blue]
      > I dont understand how is it a.operator,[/color]

      It's an operator because you normally use it like this:
      c = a + b;
      not:
      c = a.operator+(b);

      Your book should discuss the a + b form somewhere, because being able to
      write a + b is the reason you would choose to overload operator+. If you are
      only going to call it as a function you might as well just have a function
      called 'add' and call it like this: a.add(b);
      [color=blue]
      > I wish there wasa graphical
      > explanation that shows what each component represents.
      >
      > I have searched
      > many sites, and found a lot of sites with Operator overloading
      > articles, but NONE give a clear explanation to me, maybe I am just not
      > getting it. Anyone care to share any sites that can help me out or
      > give me a shot with their explanation. And by the way, if I have
      > posted to the wrong group please forgive me, I have seen so many
      > people get chewed out for asking a question here that I am sometimes
      > hesitant to post.[/color]

      They get chewed out for asking off-topic questions. Your question is
      on-topic.
      [color=blue]
      > I know some questions rightfully dont belong here, I
      > hope I am in the right place.[/color]

      Yes, you are.

      BTW, these kinds of operator overloads are normally implemented as global
      functions instead of as class member functions, because there are advantages
      in using global functions. I hope your book covers that.

      DW



      Comment

      Working...