vtkSmartPointer vtk的智能指针

是一个模板类。

template <class T>
class vtkSmartPointer: public vtkSmartPointerBase

{

  // Description:
  // Assign object to reference.  This removes any reference to an old
  // object.
  template <class U>
  vtkSmartPointer& operator=(const vtkSmartPointer<U>& r)
    {
    this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
    return *this;
    }

vtkSmartPointer重载了赋值操作符,可以在vtkSmartPointer对象之间进行赋值。在赋值过程中,vtkSmartPointer会自动控制其内部对象指针Object的引用计数加1

  // Description:
  // Create an instance of a VTK object.
  static vtkSmartPointer<T> New()
    {
    return vtkSmartPointer<T>(T::New(), NoReference());
    }

  // Get the contained pointer.
  T* GetPointer() const
    {
    return static_cast<T*>(this->Object);
    }

  // Description:
  // Provides normal pointer target member access using operator ->.
  T* operator->() const
    {
    return static_cast<T*>(this->Object);
    }

重载了‘->’操作符,返回实际的模板类型的的对象,因此可以方便的访问对象的成员函数。如light->SetColor()

class VTKCOMMONCORE_EXPORT vtkSmartPointerBase

  vtkObjectBase* Object;

vtkSmartPointer类中定义一个vtkObjectBase类型的指针对象Object,用于存储智能指针中实际生成的对象。


版权声明:本文为ermu114原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。