且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++ const方法对非常量指针成员

更新时间:2022-06-01 01:04:46

这样的东西,可能是:

template <typename T>
class deep_const_ptr {
  T* p_;
public:
  deep_const_ptr(T* p) : p_(p);

  T* operator->() { return p_;}
  const T* operator->() const { return p_;}
};

class A {
  deep_const_ptr<B> b = new B;
};

deep_const_ptr 的一个$ c>的const方法,并像 T * const T * const >在非const方法中。进一步丰富课程将留给读者。

deep_const_ptr behaves like a const T* const pointer in A's const methods, and like T* in non-const methods. Fleshing the class out further is left as an exercise for the reader.