Reputation: 1421
Now I've got this peculiar bug that seemed weird. I have a class called ENGComponent that will push itself up into one of 5 private static vectors based on its priority. The vector is gotten by calling GetComponentList(priority). This is done in the constructor. But after we get out of the constructor, the pushing back is ignored and the vector got 0 item in it. Here's the code:
ENGComponent:
#include "../../inc/engine/eng_component.h"
#include <vector>
#include <iostream>
#include <string>
#include "../../inc/engine/eng_logger.h"
//Static member var inits
std::vector<ENGComponent*> ENGComponent::m_ComponentList_1 = std::vector<ENGComponent*>();
std::vector<ENGComponent*> ENGComponent::m_ComponentList_2 = std::vector<ENGComponent*>();
std::vector<ENGComponent*> ENGComponent::m_ComponentList_3 = std::vector<ENGComponent*>();
std::vector<ENGComponent*> ENGComponent::m_ComponentList_4 = std::vector<ENGComponent*>();
std::vector<ENGComponent*> ENGComponent::m_ComponentList_5 = std::vector<ENGComponent*>();
ENGComponent::ENGComponent( const std::string& name,
Priority priority,
ENGObject* owner): m_Name(name),
m_Priority(priority),
m_pOwnerObject(owner)
{
std::vector<ENGComponent*> compList = GetComponentList(m_Priority);
if (compList == m_ComponentList_5)
m_Priority = PRIORITY_5;
compList.push_back(this);
}
std::vector<ENGComponent*>& ENGComponent::GetComponentList(Priority priority)
{
switch(priority)
{
case PRIORITY_1:
return m_ComponentList_1;
case PRIORITY_2:
return m_ComponentList_2;
break;
case PRIORITY_3:
return m_ComponentList_3;
break;
case PRIORITY_4:
return m_ComponentList_4;
break;
case PRIORITY_5:
return m_ComponentList_5;
break;
default:
//Error! TODO: Log error, change to priority 5 and move on
//TODO: LOG error
std::string errMessage = "Component priority unknown! Returning priority 5...";
ENGLogger::Log(errMessage, ENGLogger::LogLevel::ERROR);
return m_ComponentList_5;
}
}
Now, if after I instantiate the ENGComponent object, I called ENGComponent::GetComponentList(priority) anywhere else, the size of the returned m_ComponentList_X is always 0, even when I had pushed the object back. Now here comes the weird thing. If I skipped the whole priority thing, and push directly to the vector, it works just fine (ie. the vector's size incremented by one and the object is successfully pushed back). Even when I called GetComponentList() from outside the object. Like this:
ENGComponent::ENGComponent( const std::string& name,
Priority priority,
ENGObject* owner): m_Name(name),
m_Priority(priority),
m_pOwnerObject(owner)
{
m_ComponentList_5.push_back(this);
}
So what did I do wrong here? Can somebody please tell me? Thanks in advance.
Upvotes: 0
Views: 86
Reputation: 747
Although GetComponentList
is returning reference of the vector, you are storing it into compList
as a separate copy. Hence any element addition into compList
does not get added to the original vector.
You need to store the vector into a reference:
std::vector<ENGComponent*>& compList = GetComponentList(m_Priority);
So any modifications to compList
would reflect to the original vector.
There is nothing weird about:
m_ComponentList_5.push_back(this);
Here you are making modification directly using the vector. Hence this works fine.
Upvotes: 1
Reputation: 644
You shuold use reference:
std::vector<ENGComponent*>& compList = GetComponentList(m_Priority);
Upvotes: 0