Const-correctness and immutable allocated objects

前端 未结 3 1788
难免孤独
难免孤独 2021-01-18 10:05

During a recent discussion (see comments to this answer), R.. recommended to never create aliases for pointer-to-const types as you won\'t be able to deallocat

3条回答
  •  广开言路
    2021-01-18 10:43

    If you return a const-qualified pointer, the semantics are that the caller of your function is not permitted to modify the object in any way. That includes freeing it, or passing it to any function in your library that would in turn free it. Yes I'm aware that you could cast away the const qualifier, but then you're breaking the contract that const implies.

提交回复
热议问题