#include<vtkObjectFactory.h>
#include <iostream>
#include<vtkObject.h>
#include<vtkImageData.h>
#include<vtkImageActor.h>
#include<vtkSmartPointer.h>
class MyClassA :public vtkObject
{
public:
vtkTypeMacro(MyClassA, vtkObject);
static MyClassA* New();
void Setactor(vtkSmartPointer<vtkImageActor> acto);
protected:
MyClassA();
~MyClassA() override;
private:
vtkSmartPointer<vtkImageActor> actor1;
//vtkSmartPointer<vtkImageActor> actor2;
vtkSmartPointer<vtkImageData> image;
MyClassA(const MyClassA&) = delete;
void operator=(const MyClassA&) = delete;
};
vtkStandardNewMacro(MyClassA);
MyClassA::MyClassA()
{
actor1 = nullptr;
image = vtkSmartPointer<vtkImageData>::New();
//actor2=vtkSmartPointer<vtkImageActor> ::New();
cout << "MyClassA构造" << endl;
}
MyClassA::~MyClassA()
{
cout << "MyClassA析构" << endl;
}
void MyClassA::Setactor(vtkSmartPointer<vtkImageActor> acto) {
//
actor1 = acto;
//actor2->Print(cout << actor2);
}
class MyClassB :public vtkObject
{
public:
vtkTypeMacro(MyClassB, vtkObject);
static MyClassB* New();
vtkSmartPointer<MyClassA> CreateMyClass();
MyClassA* classAA;
//vtkSmartPointer<MyClassA> classAA;
vtkSmartPointer<vtkImageActor> actor;
protected:
MyClassB();
~MyClassB() override;
private:
MyClassB(const MyClassB&) = delete;
void operator=(const MyClassB&) = delete;
};
vtkStandardNewMacro(MyClassB);
MyClassB::MyClassB()
{
cout << "MyClassB构造" << endl;
classAA = vtkSmartPointer<MyClassA>::New();
actor= vtkSmartPointer<vtkImageActor>::New();
}
MyClassB::~MyClassB()
{
cout << "MyClassB析构" << endl;
}
int main()
{
auto B = vtkSmartPointer<MyClassB>::New();
B->classAA ->Setactor(B->actor);
cout << "成功返回"<<endl;
}
Here is a code, including a bug. The reason for the bug is that MyClassA is a common pointer, and smart pointer is used during instantiation. This code has randomness and can run normally occasionally. I want to know why randomness occurs?