overloading the + operator:

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::eek: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
 
M

Marc Durufle

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.
 
V

Victor Bazarov

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&);

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::eek: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*?

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
 
D

David.H

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::eek: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*?

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top