How to cast away the volatile-ness?

后端 未结 1 616
长发绾君心
长发绾君心 2021-02-01 14:15

How to cast away the volatile-ness? Which c++ style cast should I use?

1条回答
  •  暖寄归人
    2021-02-01 14:44

    Use const_cast.

    For example,

    volatile sample *pvs = new sample();
    sample *ps = const_cast(pvs); //casting away the volatile-ness
    

    That is, const_cast is used to cast away both const-ness as well as volatile-ness. Unfortunately, its name doesn't contain the term "volatile". Maybe, that is because the keyword const is more common in use than the keyword volatile. As one of the comment says, cv_cast would have been more appropriate name!

    0 讨论(0)
提交回复
热议问题