I apologize if this question has been answered before, I couldn't find an answer anywhere.
I'm having trouble with keeping const correctness.
I have the following definitions.
struct C {
int x,y;
};
class A {
public:
C c;
C& getC() { return c; }
};
class B {
public:
void forwardInfo(const A& a) const {
const C& c = a.getC(); // <= compiler complains here
daoObject.updateTableX(c.x,c.y);
}
};
The compiler complains that object a is const and therefore can't call a.getC() since that method is not const.
However I'm assigning the result to a const reference. Right now I just cast the a object a non const reference like so:
const C& c = ((A&)a).getC();
This works, but is not elegant. Is there a better way without changing constness. I believe that for object A returning C as not const is reasonable since it is meant to be changed.
Also the forwardInfo should be const since I don't want to change anything.
I'm not sure why the compiler doesn't let me do this and is there a better way?
Ps. I could use getter/setter methods, but class C is meant to be used as a data bucket for passing to a database.
Thank you.
Fixed: bracket location in cast and incorrect cast.