[英]I can't assign an object to an object array
I just wrote my first copy constructor and copy operator, and I'm trying to assign an object instance to an array like so:我刚刚编写了我的第一个复制构造函数和复制运算符,我正在尝试将一个 object 实例分配给一个数组,如下所示:
Agent agent = Agent(navmesh, rb, m_maxPathSize);
Agent tmp = agent; // DEBUG
m_agents[idx] = agent;
The copy constructor seems to be working fine, since tmp
is a perfect copy of agent
(with a newly assigned m_path
pointer).复制构造函数似乎工作正常,因为
tmp
是agent
的完美副本(具有新分配的m_path
指针)。 But when I assign agent
to m_agents[idx]
, the latter consists of what I'd expect from the default constructor ( m_path == 0
, m_alive == false
).但是当我将
agent
分配给m_agents[idx]
时,后者包含我对默认构造函数的期望( m_path == 0
, m_alive == false
)。
My constructors look like this:我的构造函数如下所示:
Agent() { m_path = 0; m_alive = false; };
Agent::Agent(NavMeshNavigator* navmesh, RigidBody* rb, int maxPathSize)
: m_rb(rb), m_navmesh(navmesh), m_maxPathCount(maxPathSize)
{
m_path = new float3[maxPathSize];
};
Agent::Agent(const Agent &a)
{
memcpy(this, &a, sizeof(Agent));
if (m_path)
{
float3* oldptr = m_path;
m_path = new float3[m_maxPathCount];
memcpy(m_path, oldptr, m_maxPathCount * sizeof(float3));
}
}
Agent& Agent::operator=(const Agent &a) { return Agent(a); }
Agent::~Agent() { if (m_path) delete[] m_path; };
...
protected:
float3* m_path;
bool m_alive = true;
The constructor allocates memory for m_path
using new[]
, the destructor frees it with delete[]
, the copy operator calls the copy constructor, and the copy constructor first memcopies the original before allocating a new m_path
array.构造函数使用
new[]
为m_path
分配 memory ,析构函数使用delete[]
释放它,复制运算符调用复制构造函数,并且复制构造函数在分配新的m_path
数组之前首先复制原始文件。
In my test case, idx == 0
, so that can't be it.在我的测试用例中,
idx == 0
,所以不可能。 I originally used malloc
instead of new[]
but got the same results.我最初使用
malloc
而不是new[]
但得到了相同的结果。 I'd say the problem is in my copy constructor/operator since I have no experience with that, but then why does it work perfectly on tmp
?我会说问题出在我的复制构造函数/操作符上,因为我没有这方面的经验,但是为什么它在
tmp
上完美运行?
EDIT: The m_agents array is declared and destroyed like this:编辑: m_agents 数组是这样声明和销毁的:
NavMeshAgents(int maxAgents, int maxAgentPathSize)
: m_maxAgents(maxAgents), m_maxPathSize(maxAgentPathSize)
{
m_agents = new Agent[maxAgents];
};
~NavMeshAgents() { if (m_agents) delete[] m_agents; m_agents = 0; };
As @Evg @HolyBlackCat and @Adrian-Reinstate-Monica explained in the comments, new[]
calls the default constructor for all its members.正如@Evg @HolyBlackCat 和@Adrian-Reinstate-Monica 在评论中解释的那样,
new[]
为其所有成员调用默认构造函数。 Agent tmp = agent
calls the copy constructor, whereas tmp = agent
would've called the assignment operator ( tmp.operator=(agent)
). Agent tmp = agent
调用复制构造函数,而tmp = agent
将调用赋值运算符( tmp.operator=(agent)
)。 My assignment operator was wrong, it should initialize this
(and then return *this
rather than return an instance.我的赋值运算符错了,它应该初始化
this
(然后返回*this
而不是返回一个实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.