Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A problem with "const" when overloading operator ==

Reply
Thread Tools

A problem with "const" when overloading operator ==

 
 
Paulo da Silva
Guest
Posts: n/a
 
      12-05-2010
Please consider the following fragment:

using namespace std;

class C1
{ public:
bool operator == (C1 const &s)
{ return false;
}
};

class C2
{ private:
C1 c;
public:
bool comp(C2 const * s) const
{ return this->c==s->c;
}
};

I got the following error
"passing ‘const C1’ as ‘this’ argument of ‘bool C1:perator==(const
C1&)’ discards qualifiers"

at line "{ return this->c==s->c;".

Is there any way to fix the problem acting on operator == definition, or
in the class C1?

Thanks.
 
Reply With Quote
 
 
 
 
Tiib
Guest
Posts: n/a
 
      12-06-2010
On Dec 6, 1:05*am, Paulo da Silva <(E-Mail Removed)>
wrote:
> Please consider the following fragment:
>
> using namespace std;
>
> class C1
> { * * * public:
> * * * * * * * * bool operator == (C1 const &s)


Should be:
* * * * * * * * bool operator == (C1 const &s) const

> * * * * * * * * { * * * return false;
> * * * * * * * * }
>
> };
>
> class C2
> { * * * private:
> * * * * * * * * C1 c;
> * * * * public:
> * * * * * * * * bool comp(C2 const * s) const
> * * * * * * * * { * * * return this->c==s->c;
> * * * * * * * * }
>
> };
>
> I got the following error
> "passing const C1 as this argument of bool C1:perator==(const
> C1&) discards qualifiers"
>
> at line "{ return this->c==s->c;".
>
> Is there any way to fix the problem acting on operator == definition, or
> in the class C1?
>
> Thanks.


 
Reply With Quote
 
 
 
 
Paulo da Silva
Guest
Posts: n/a
 
      12-06-2010
Em 06-12-2010 00:02, Tiib escreveu:
> On Dec 6, 1:05 am, Paulo da Silva <(E-Mail Removed)>
> wrote:
>> Please consider the following fragment:
>>
>> using namespace std;
>>
>> class C1
>> { public:
>> bool operator == (C1 const &s)

>
> Should be:
> bool operator == (C1 const &s) const
>

Oops! I tried that, but unfortunately I forgot to change the prototype
of the original program (.h). The error changed but I didn't see that
because the very few error lines available on the eclipse do not have
attracted my attention.

I didn't program in c++ for the last couple of years and I am making
trivial mistakes .

Thank you very much and sorry for the trivial question.

....
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments