I want to make interface with getter which calls virtual method from child. But now i have problems: implementation of const method in child shadows parent one. Why? I have tried to make parent Get non virtual (not working). Then I have tried switch const and mutable implementation -- same behaviour. But if I implement both it seems fine.
struct A {
virtual const int& Get() const = 0 ;
virtual int& Get() {
return const_cast<int&>(const_cast<const A*>(this)->Get());
}
virtual ~A() = default;
};
struct B : A{
int i{42};
const int& Get() const override { return i; }
};
int main(){
B b;
b.Get() = 32; // error
static_cast<A&>(b).Get() = 32; //ok
std::cout << b.i;
return 0;
}