Say I have a class Foo
that has a member function which returns a non-const reference, that itself runs a member function that uses a const this
pointer:
class Foo{
public:
Foo& display(std::ostream& os) { do_display(os); return *this; }
private:
void do_display(std::ostream& os) const { os << contents; }
std::string contents;
}
When display
runs do_display
, the this
pointer gets implicitly converted to a pointer to const. Why is it, then, that when do_display
terminates, display
is still able to change the object it was called on? As far as I know, it's not possible to assign a pointer to const to a pointer to non-const normally. Any insight is appreciated.