当我返回它的引用时,静态std :: vector会在超出范围时取消

时间:2014-11-07 02:00:29

标签: c++ vector reference

现在我得到了这个奇怪的怪物。我有一个名为ENGComponent的类,它将根据其优先级将自己推入5个私有静态向量之一。通过调用GetComponentList(优先级)获取向量。这是在构造函数中完成的。但是在我们离开构造函数之后,忽略了推回,向量得到0项。这是代码:

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;
    }
} 

现在,如果在我实例化ENGComponent对象后,我在其他地方调用了ENGComponent :: GetComponentList(priority),则返回的m_ComponentList_X的大小始终为0,即使我将对象推回。现在这里出现了奇怪的事情。如果我跳过整个优先级事物,并直接推送到向量,它就可以正常工作(即向量的大小增加1并且对象被成功推回)。甚至当我从对象外部调用GetComponentList()时。像这样:

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);
}

那我在这里做错了什么?有人可以告诉我吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

虽然GetComponentList正在返回向量的引用,但您将其作为单独的副本存储到compList中。因此,compList中的任何元素添加都不会添加到原始向量中。

您需要将矢量存储到参考中:

std::vector<ENGComponent*>& compList = GetComponentList(m_Priority);

因此对compList的任何修改都会反映到原始向量。

没有什么奇怪的:

m_ComponentList_5.push_back(this);

在这里,您可以使用向量直接进行修改。因此,这很好。

答案 1 :(得分:0)

您应该使用参考:

std::vector<ENGComponent*>& compList = GetComponentList(m_Priority);