473,388 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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.
Jul 19 '05 #1
3 2575
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.