What is the static_cast runtime overhead if adding constness while keeping the same type?

前端 未结 1 1708
不知归路
不知归路 2021-01-24 05:49

I find it irritating that I can call non-const functions of an object if I have a pointer to this object. I cannot let the pointer be a const pointer because there are also non-

1条回答
  •  清歌不尽
    2021-01-24 06:37

    The runtime overhead is essentially a pointer copy, which may be optimised out altogether.

    But in your case, I'd consider changing int m_i; to mutable std::atomic m_i; and make increment constant in the base class too. It looks like a reference counter and my way allows you to (i) retain const-correctness and (ii) be thread safe. I'd also consider changing int to unsigned to avoid undefined behaviour if m_i gets too big.

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