小编ppl*_*ppl的帖子

C++ - 我们为什么要使用operator - >来访问SmartPtr的成员函数?

问题在最后两行代码中给出.

template<class T>                    // template class for smart
class SmartPtr {                     // pointers-to-T objects
public:
  SmartPtr(T* realPtr = 0);

  T* operator->() const;
  T& operator*() const;

  T* Detach( void )
  {
    T* pData = pointee;
    pointee = NULL;
    return pData;
  }  

private:
  T *pointee;
  ...
};

class TestClass {}

SmartPtr<TestClass> sPtr(new TestClass);

TestClass* ptrA = sPtr->Detach();
// why I always see people use this method to access member functions of a Smart pointer.
// We can use sPtr-> b/c we have …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
358
查看次数

标签 统计

c++ ×1