Question on the class

1
2
3
4
5
const A& A::operator=(const A& rhs)
{
  int k = rhs.get_k();
  /*.....other operation.... */
}

error C2662: 'A::get_k' : cannot convert 'this' pointer from 'const A' to 'A&'
Conversion loses qualifiers

When I run the program it alerts me with the above warning. What's wrong with it?
Thanks all!

p.s. .get_k() is a function returning an integer
I think you need to make the return value non-const:

1
2
3
4
5
6
7
8
A& A::operator=(const A& rhs)
{
  int k = rhs.get_k();

  /*.....other operation.... */

  return *this;
}


Last edited on
Oh, actually for your problem I think you need to declare get_k() as a const function:

1
2
3
4
class A
{
    int get_k() const;
};


That means the function get_k() is callable from a const reference.
Topic archived. No new replies allowed.