当我返回其引用时,如果超出范围,则静态std::vector将取消

Static std::vector cancels out if out of scope when I returned its reference

本文关键字:静态 取消 std vector 范围 返回 引用 如果      更新时间:2023-10-16

现在我遇到了一个奇怪的bug。我有一个名为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(优先级),则返回的m_ComponentList_X的大小始终为0,即使我已将对象推回。现在奇怪的事情来了。如果我跳过了整个优先级的事情,直接推到向量上,它会很好地工作(即向量的大小增加一,对象被成功地推回来)。即使我从对象外部调用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);
}

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

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

您需要将矢量存储到引用中:

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

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

没有什么奇怪的

m_ComponentList_5.push_back(this);

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

您可以使用引用:

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