当我返回其引用时,如果超出范围,则静态std::vector将取消
Static std::vector cancels out if out of scope when I returned its reference
现在我遇到了一个奇怪的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);
相关文章:
- 挂起和取消挂起一个文件DLL
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- #为""定义宏;静态";针对不同的上下文
- cmake如何在fedora工作站中找到boost静态库包
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- cmake在我的项目中所需的所有静态库都不成功
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何取消对nullptr的屏蔽,返回正确的对象
- 基于boost的程序的静态链接——zlib问题
- 在静态库中嵌入类方法
- C++取消引用指针.为什么会发生变化
- 在push_back到静态向量之后C++取消定位
- 加载程序如何从动态模块分配/取消分配静态数据
- 取消引用静态变量时的链接错误
- 取消定义静态函数的引用错误
- 当我返回其引用时,如果超出范围,则静态std::vector将取消
- VS2012静态代码分析器在取消分配内存向量时给出误报
- 取消引用非静态成员函数C++
- 指向静态数组,但在不使用*的情况下取消引用