vtk智能指针->对智能指针的理解和使用

本文介绍了VTK中智能指针的使用,包括引用计数的概念,强调了智能指针在对象生命周期管理中的作用。通过智能指针,可以自动管理对象的创建与析构,避免内存泄漏。VTK提供了两种创建对象的方式:New()方法与智能指针vtkSmartPointer。使用vtkSmartPointer时,无需手动调用Delete(),智能指针会自动完成引用计数的增减。
摘要由CSDN通过智能技术生成

学习代码:

#include <vtkSmartPointer.h>
#include <vtkBMPReader.h>
#include <vtkImageData.h>
#include <vtkObject.h>

// MyFunction函数:演示智能指针可以作为函数返回值
vtkSmartPointer<vtkImageData> MyFunction()
{
    vtkSmartPointer<vtkImageData> myObject = vtkSmartPointer<vtkImageData>::New();
    std::cout<<"MyFunction::myObject reference count = "<<myObject->GetReferenceCount()<<std::endl;
    return myObject;
}

//测试文件:data/VTK-logo.bmp
int main(int argc, char* argv[])
{

    //演示引用计数:
    vtkSmartPointer<vtkBMPReader> reader = vtkSmartPointer<vtkBMPReader>::New();
    reader->SetFileName("C:\\Users\\Administrator\\Desktop\\vtk1\\Examples\\Examples\\Chap02\\data\\VTK-logo.bmp");
    reader->Update();

    std::cout<<"Reference Count of reader->GetOutput (Before Assignment) = "
        <<reader->GetOutput()->GetReferenceCount()<<std::endl;

    vtkSmartPointer<vtkImageData> image1 = reader->GetOutput();
    std::cout<<"Reference Count of reader->GetOutput (Assign to image1) = "
        <<reader->GetOutput()->GetReferenceCount()<<std::endl;
    std::cout<<"Reference Count of image1 = "
        <<image1->GetReferenceCount()<<std::endl;

    vtkSmartPointer<vtkImageData> image2 = reader->GetOutput();
    std::cout<<"Reference
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值