B
Buster Copley
David.H said: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:perator+(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*?
It can.
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.
gph is a reference to a const object, getx () is a non-const
member function...
Question 2:
I?d also like to know the reason behind using const member functions.
.... and non-const member functions cannot be invoked on const objects.
Declare getx as a const member function:
int getx (void) const { return x; }
Thank you for your attention.
No problem.
Buster