By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 739 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

overloading the + operator:

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
David.H wrote:
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*?
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

Jul 19 '05 #2

P: n/a
"David.H" <ed****@intnet.mu> wrote...
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&);
If performing this operation doesn't involve changing the lefthand
side object, I'd declare this function 'const':

Graph operator+(const Graph&) const;
int getx(void) { return x; }
int gety(void) { return y; }
Same for those.

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*?
I don't understand the question. It _is_ const, why do you ask "why it
can't"?
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.
'getx' is not 'const'. You're calling it for 'gph', which _is_ const.
Declare both 'getx' and 'gety' as const.
Question 2:
I?d also like to know the reason behind using const member functions.


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
Jul 19 '05 #3

P: n/a
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;
};


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
--------------------------
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.