I'm learning to despise const
.
struct b;
struct a {
b* p;
void nonConst() { p = nullptr;}
b* getP() const { return p;}
};
struct b {
a *p;
a *getP() const { return p;}
};
void func (const a *ii) {
b *uRef = ii->getP();
//dear god, we've just obtained a pointer to a non-const a, starting with const a
a *iii = uRef->getP();
//and we've just modified ii, a const object
iii->nonConst();
}
int main() {
a *i = new a;
b *u = new b;
i->p = u;
u->p = i;
func(i);
}
Could this induce any undefined behavior? If not, is it breaking any const
rules? If not that, why is this
treated as a const
pointer to const
data, and not just a const
pointer to non-const
data?