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

前端 未结 3 637
伪装坚强ぢ
伪装坚强ぢ 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 04:08

    Be aware that if you do this and the object really is const, then modifying it after casting away the constness is undefined behaviour.

    fgBlocks.CopyInto(const_cast(backUpCopy));
    

    Same thing for the other one:

    const_cast(backUpCopy).RemoveAll(true);
    

提交回复
热议问题