How do you cast away const'ness when the function takes a reference to the object (and access non-const methods)?

前端 未结 3 657
伪装坚强ぢ
伪装坚强ぢ 2021-01-13 03:32

I have a back up copy of data that I would like to protect so I made it const. I need to violate that constness on two occassions, once to store vi

3条回答
  •  遥遥无期
    2021-01-13 03:53

    You can circumvent this problem by simply marking all methods as const, except RemoveAll and CopyFrom, the latter being made to be a method of BlkArray that either implements the logic or passes *this to CopyInto.

    To be more secure about who can clear the data / copy new stuff into it, you may make those methods private and declare the necessary classes as friends, or use the passkey pattern to protect those two methods.

提交回复
热议问题