我正在尝试创建一个Actor
指向另一个Actor
对象的指针,如下所示:
Actor other = Actor();
Actor* ptr = &other;
然后,当我尝试时delete ptr
,它会导致运行时错误:
Program.exe 已触发断点
但是,当我创建一个新Actor
的而不是分配ptr
给 的引用时other
,我可以安全地delete
使用它而不会出现任何错误:
Actor* ptr = new Actor();
delete ptr;
我不明白问题是什么。
这是我的Actor
班级的样子:
演员.h:
class Actor
{
private:
unsigned id;
string name;
vector< unique_ptr< Behaviour > > allBehaviours;
public:
Actor();
virtual ~Actor();
void Init(); // Go through all the behaviors and call their Inits and Ticks
void Tick();
}
演员.cpp:
#include "Planet.h"
#include "Behaviour.h"
#include "Actor.h"
Actor::Actor()
{
win.GetCurrentPlanet()->AddActor(this);
planet = win.GetCurrentPlanet();
}
Actor::~Actor()
{
printf("Deleted Actor!");
if (allBehaviours.size() > 0)
allBehaviours.clear();
}
// Init and Tick and some getters, setters for name and id
我已经搜索过,发现了The Rule of Three,但我不明白在设置这样的指针时使用了什么运算符:
Actor other = Actor();
Actor* ptr = &other;
我认为它是复制构造函数,但是如何为我的程序实现它呢?