C++ 空向量的全局向量
我有一个有两个全局向量的类。一个是结构向量,另一个是结构向量。是这样的:C++ 空向量的全局向量,c++,class,vector,C++,Class,Vector,我有一个有两个全局向量的类。一个是结构向量,另一个是结构向量。是这样的: class CNewDisplay: { protected: //Vector of structures struct FP { .... } std::vector<FP> FP_list; //Vector of vectors of structures struct INSTRUCTIONS {... } std::vector<INSTRUCTIONS> Instruction_
class CNewDisplay:
{
protected:
//Vector of structures
struct FP
{
....
}
std::vector<FP> FP_list;
//Vector of vectors of structures
struct INSTRUCTIONS
{...
}
std::vector<INSTRUCTIONS> Instruction_list;
std::vector< std::vector<INSTRUCTIONS> > Past_Instructions;
public:
std::string loop_planes(..)
void fill_instruction_vector(...)
填充过去的\u指令的函数
:
void CNewDisplay::fill_instruction_vector(...){
....
Past_Instructions.push_back(Instruction_list);
Instruction_list.clear();
//The size of the `Past_Instructions` is correct here
}
在py\u embed()
函数退出后,过去的指令
是空的。我想这与void py\u embed(void)
不在类公共函数中有关,但我不明白为什么,因为它是void fill\u指令向量(…)
调用并填充过去的\u指令
向量,因为过去的\u指令
是全局的。有人能给我解释一下吗
注意:将向量传递给类的公共成员不起作用。我不明白为什么您有std::vector,然后只有vector。我已经更正了,谢谢您的帮助advice@40two向量不会传递到
填充指令\u vector()
,而是在函数内部访问。这就是你要问的吗?从表面上看,指令列表
和过去的指令
根本不是全局指令,而是类CNewDisplay
的成员,具有受保护的
访问权限。现在在“py_embed()”中,您声明了一个CNewDisplay
对象(即dlg
),该对象将位于函数“py_embed()”的本地范围内(即py_embed()
退出dlg
后不再存在)。我的问题是,既然dlg
是“死的”,除了要填充的向量之外,还有什么向量?过去的\u指令应该是来自多个CNewDisplay实例的共享/组合历史吗?如果是这样,您只需要将过去的_指令声明为“static”,并在.cpp文件中实例化它。
void CNewDisplay::fill_instruction_vector(...){
....
Past_Instructions.push_back(Instruction_list);
Instruction_list.clear();
//The size of the `Past_Instructions` is correct here
}