A problem with "const" when overloading operator ==

Discussion in 'C++' started by Paulo da Silva, Dec 5, 2010.

  1. 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::eek: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.
    Paulo da Silva, Dec 5, 2010
    #1
    1. Advertising

  2. Paulo da Silva

    Öö Tiib Guest

    On Dec 6, 1:05 am, Paulo da Silva <>
    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::eek: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.
    Öö Tiib, Dec 6, 2010
    #2
    1. Advertising

  3. Em 06-12-2010 00:02, Öö Tiib escreveu:
    > On Dec 6, 1:05 am, Paulo da Silva <>
    > 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.

    ....
    Paulo da Silva, Dec 6, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Clacy
    Replies:
    15
    Views:
    2,681
    Kanenas
    May 30, 2005
  2. Replies:
    11
    Views:
    1,102
  3. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,116
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    887
    fungus
    Oct 31, 2008
Loading...

Share This Page