overloading the + operator:

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David.H

    overloading the + operator:

    Good evening everyone,

    Im using this code to get an idea on overloading the + operator:

    class Graph {
    public:
    Graph(void);
    Graph(int valX, int valY);
    Graph operator+(const Graph&);
    int getx(void) { return x; }
    int gety(void) { return y; }

    private:
    int x, y;
    };

    Graph Graph::operator +(const Graph& gph) {
    Graph res( (this->getx() + gph.getx()), (this->gety() + gph.gety()) );
    return res;
    }

    Question 1:
    I would like to understand why the parameter passed to operator+ member
    function cant be type specified as *const*?

    In the code above, my compiler GCC 3.2.2 on Linux 2.4.21 gives the following compilation
    error: passing const Graph as this argument of int Graph::getx()
    discards qualifiers.

    Question 2:
    I?d also like to know the reason behind using const member functions.

    Thank you for your attention.
  • Buster Copley

    #2
    Re: overloading the + operator:

    David.H wrote:[color=blue]
    > Good evening everyone,
    >
    > Im using this code to get an idea on overloading the + operator:
    >
    > class Graph {
    > public:
    > Graph(void);
    > Graph(int valX, int valY);
    > Graph operator+(const Graph&);
    > int getx(void) { return x; }
    > int gety(void) { return y; }
    >
    > private:
    > int x, y;
    > };
    >
    > Graph Graph::operator +(const Graph& gph) {
    > Graph res( (this->getx() + gph.getx()), (this->gety() + gph.gety()) );
    > return res;
    > }
    >
    > Question 1:
    > I would like to understand why the parameter passed to operator+ member
    > function cant be type specified as *const*?[/color]

    It can.
    [color=blue]
    > In the code above, my compiler GCC 3.2.2 on Linux 2.4.21 gives the following compilation
    > error: passing const Graph as this argument of int Graph::getx()
    > discards qualifiers.[/color]

    gph is a reference to a const object, getx () is a non-const
    member function...
    [color=blue]
    > Question 2:
    > I?d also like to know the reason behind using const member functions.[/color]

    .... and non-const member functions cannot be invoked on const objects.
    Declare getx as a const member function:
    int getx (void) const { return x; }
    [color=blue]
    > Thank you for your attention.[/color]

    No problem.
    Buster

    Comment

    • Victor Bazarov

      #3
      Re: overloading the + operator:

      "David.H" <edavid@intnet. mu> wrote...[color=blue]
      > Good evening everyone,
      >
      > Im using this code to get an idea on overloading the + operator:
      >
      > class Graph {
      > public:
      > Graph(void);
      > Graph(int valX, int valY);
      > Graph operator+(const Graph&);[/color]

      If performing this operation doesn't involve changing the lefthand
      side object, I'd declare this function 'const':

      Graph operator+(const Graph&) const;
      [color=blue]
      > int getx(void) { return x; }
      > int gety(void) { return y; }[/color]

      Same for those.
      [color=blue]
      >
      > private:
      > int x, y;
      > };
      >
      > Graph Graph::operator +(const Graph& gph) {
      > Graph res( (this->getx() + gph.getx()), (this->gety() + gph.gety()) );
      > return res;
      > }
      >
      > Question 1:
      > I would like to understand why the parameter passed to operator+ member
      > function cant be type specified as *const*?[/color]

      I don't understand the question. It _is_ const, why do you ask "why it
      can't"?
      [color=blue]
      > In the code above, my compiler GCC 3.2.2 on Linux 2.4.21 gives the[/color]
      following compilation[color=blue]
      > error: passing const Graph as this argument of int Graph::getx()
      > discards qualifiers.[/color]

      'getx' is not 'const'. You're calling it for 'gph', which _is_ const.
      Declare both 'getx' and 'gety' as const.
      [color=blue]
      > Question 2:
      > I?d also like to know the reason behind using const member functions.[/color]

      The reason is very simple: if the member does not change the object it
      is called for, it should be 'const'. Declaring a member function 'const'
      states the intention: the member function is not going to change the
      object. If then, when implementing that function, you attempt to change
      the object [designated by 'this' pointer], the compiler should complain.

      Victor


      Comment

      • Marc Durufle

        #4
        Re: overloading the + operator:

        [color=blue]
        > Im using this code to get an idea on overloading the + operator:
        >
        > class Graph {
        > public:
        > Graph(void);
        > Graph(int valX, int valY);
        > Graph operator+(const Graph&);
        > int getx(void) { return x; }
        > int gety(void) { return y; }
        >
        > private:
        > int x, y;
        > };[/color]

        you had better to put :

        class Graph {
        public:
        Graph(void);
        Graph(int valX, int valY);
        Graph operator+(const Graph&);
        int getx(void) const { return x; }
        int gety(void) const { return y; }

        private:
        int x, y;
        };

        By this way, you declarate the members functions getx and gety as
        functions, that don't modify the object.
        When you declare an object const in a method, and if you use a function
        which modify it, you will have this bug "discards qualifier"
        My advice is to put the keyword const to all member functions which
        don't modify the object.


        --
        Marc Durufle
        Inria Rocquencourt
        Tel : 01 39 63 56 27
        --------------------------

        Comment

        Working...