Bwmat的回答有效:
#include <iostream>
#include <vtkObject.h>
#include <vtkObjectFactory.h>
#include <vtkSmartPointer.h>
class vtkMyClass : public vtkObject {
public:
vtkTypeMacro(vtkMyClass,vtkObject);
void PrintSelf(ostream& os, vtkIndent indent){}
static vtkMyClass * New();
protected:
vtkMyClass();
~vtkMyClass();
};
vtkStandardNewMacro(vtkMyClass)
vtkMyClass::vtkMyClass() {
std::cerr << "constructor called\n";
}
vtkMyClass::~vtkMyClass() {
std::cerr << "destructor called\n";
}
int main(int argc, char ** argv) {
std::cerr << __LINE__ << '\n';
vtkSmartPointer< vtkMyClass > myObject;
std::cerr << __LINE__ << '\n';
myObject = vtkSmartPointer< vtkMyClass >::New();
std::cerr << __LINE__ << '\n';
myObject = NULL; // calls destructor
std::cerr << __LINE__ << '\n';
return 0;
}
输出:
22
24
constructor called
26
destructor called
28