C++ standard says that modifying an object originally declared const
is undefined behavior. But then how do constructors and destructors operate?
The standard doesn't really say a lot about how the implementation makes it work, but the basic idea is pretty simple: the const
applies to the object, not (necessarily) to the memory in which the object is stored. Since the ctor is part of what creates the object, it's not really an object until (sometime shortly after) the ctor returns. Likewise, since the dtor takes part in destroying the object, it's no longer really operating on a complete object either.