2014-11-07 28 views
0

現在我已經看到了這個奇怪的奇特錯誤。我有一個名爲ENGComponent的類,它將根據它的優先級將它自己推入5個私有靜態向量之一。該向量通過調用GetComponentList(priority)來獲得。這是在構造函數中完成的。但是在我們離開構造函數之後,忽略推回,並且向量中有0個項目。下面的代碼:當我返回參考時,靜態std :: vector會被取消,如果超出範圍

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,甚至當我把物體推回去時。現在來了這個奇怪的事情。如果我跳過整個優先事項,並直接推送到矢量,它就可以正常工作(即向量的大小增加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); 
} 

那麼我在這裏做錯了什麼?有人可以告訴我嗎?提前致謝。

+0

PS不需要'= std :: vector ()'位。 – 2014-11-07 02:08:03

+0

'std :: vector compList = GetComponentList(m_Priority);'這是幹什麼用的?您創建一個本地副本,當構造函數返回時它將被銷燬。 – 2014-11-07 02:08:31

+0

你的意思是'compList'是一個引用你的向量,還是它的副本?因爲現在是後者。 – Nemo 2014-11-07 02:08:59

回答

1

雖然GetComponentList是返回向量的參考,您存儲成compList作爲一個獨立的副本。因此,添加到compList中的任何元素都不會被添加到原始矢量中。

您需要矢量存儲到一個參考:

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

所以任何修改compList將反映到原來的載體。

沒有什麼奇怪的約:

m_ComponentList_5.push_back(this); 

在這裏,您正在修改直接使用矢量。因此,這工作正常。

+0

IC。感謝你的回答。我吮吸引用,我承認。更糟糕的是,我目前正在使用C#/ Objective C/JavaScript。他們的指針和引用類型的概念搞砸了我的C++。 :P – 2014-11-07 03:02:33

0

您shuold使用參考:

std::vector<ENGComponent*>& compList = GetComponentList(m_Priority); 
相關問題