2

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.

Jaap Geurts
  • 623
  • 6
  • 9
  • 3
    Casting the result to `const` as you propose won't work. It is the object `a` on which you're calling a non-`const` member that is causing the error. – Persixty Jul 06 '15 at 06:11

2 Answers2

10

You can overload "getC" method like this:

C& getC() { return c; }
const C& getC() const { return c; }
fghj
  • 8,898
  • 4
  • 28
  • 56
  • I don't think there is a need for overloading, just the second form of the function would be sufficient – Codi Jul 06 '15 at 06:47
  • The overload works. Why didn't I think of that.... I do need both forms, since sometimes I do want to change the contents of C. Thank you. – Jaap Geurts Jul 06 '15 at 07:24
0

If you are calling a function of const A& then you need to declare that function as const as well, because the compiler does not know whether the object of A will change or not, hence it cannot verify the const-ness of the parameter const A& a

struct C {
   int x,y;
};

class A {
public:
  C c;      
  const C& getC() const { return c; }
};

class B {
public:
  void forwardInfo(const A& a) const {
    const C& c = a.getC();
    daoObject.updateTableX(c.x,c.y);
  }
};

Note the line in class A const C& getC() const { return c; }

By using const, we tell the compiler that A::getC() will not modify the object it is being called on, and so this will work.

Also you need to change the return type of the function as const, as that what you require.

Note: You can assign a non-const value to a const variable, but not the other way around. const is an added property that tells the compiler that the value (or refernce address, which is also in a way a value), will not change.

      int foo1 = 5;
const int foo2 = 10;

      int& bar1 = foo1; // allowed
const int& bar2 = foo1; // allowed
      int& bar3 = foo2; // NOT allowed -> will give compile time error
const int& bar4 = foo2; // allowed
Codi
  • 511
  • 3
  • 19