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

标签 c++ vector reference

现在我遇到了这个看起来很奇怪的特殊错误。我有一个名为 ENGComponent 的类,它会根据其优先级将自己插入 5 个私有(private)静态 vector 之一。通过调用 GetComponentList(priority) 获取 vector 。这是在构造函数中完成的。但是在我们离开构造函数之后,推回被忽略并且 vector 中有 0 个项目。这是代码:

英文组件:

#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,即使我已将对象推回。现在奇怪的事情来了。如果我跳过整个优先级的事情,直接推送到 vector ,它工作得很好(即 vector 的大小增加一,对象被成功推回)。即使当我从对象外部调用 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 返回 vector 的引用,但您将它作为单独的拷贝存储到 compList 中。因此,添加到 compList 中的任何元素都不会添加到原始 vector 中。

您需要将 vector 存储到一个引用中:

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

因此对 compList 的任何修改都会反射(reflect)到原始 vector 中。

没有什么奇怪的:

m_ComponentList_5.push_back(this);

此处您直接使用 vector 进行修改。因此这很好用。

关于c++ - 当我返回其引用时,如果超出范围,静态 std::vector 将取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26792818/

相关文章:

c++ - 如何在c++中设计一个大类的头文件?

c++ - 返回一个 vector 作为参数

python - python 中的大型对称列表 : will references contribute to size in ram?

java - java中的字体类引用

c++ - 从函数返回对局部变量的 const 引用

C++ - 运算符重载和继承

c++ - 静态函数、内联和模板?

c++ - 初始化 vector (C++)

c++ - vector 调整大小与嵌套 vector 的保留

c++ - enable_shared_from_this - 空的内部弱指针?