If changing a const object is undefined behavior then how do constructors and destructors operate with write access?

前端 未结 5 1572
名媛妹妹
名媛妹妹 2020-12-11 03:48

C++ standard says that modifying an object originally declared const is undefined behavior. But then how do constructors and destructors operate?



        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 03:54

    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.

提交回复
热议问题