I want to return a const reference so I cannot change the object itself but I can call their non-const methods. But I don't know how to do it.
With pointers is easy, I can choose between a const pointer (const myclass*) or a pointer to const values (myclass const*). But with references it seems not working the same way, all I've got it is a reference to a const object so I cannot call non-const methods.
I'm sure I'm doing something wrong.
class A {
int a;
public:
auto get() const { return a; }
auto set(int i) -> void { a = i; }
};
class B {
A a_;
public:
auto get() const -> const A& { return a_; }
};
I can do:
B b;
cout << b.get().get();
But not:
B b;
b.get().set(100); // compiler error
I don't want
class B {
A a_;
public:
auto get() -> A& { return a_; }
};
B b;
A a;
b.get() = a; // I don't want this!