简体   繁体   English

如何将静态向量添加到类

[英]How can i add a static vector to a class

I have a class and i want a vector to live for the the life of the programe so i am using a static vector. 我有一个类,我想要一个向量在程序的整个生命中都活着,所以我正在使用静态向量。

Please look at my code and advise me if their is better way of using static variable. 请查看我的代码,并告诉我它们是否是使用静态变量的更好方法。

static std::vector<std::string> Stack;


class Test
{


public:
void AddStack(std::string str)
void PopStack()

};


void Test::AddStack(std::string str)
{

Stack.insert(Stack.end(),str.begin(),str.end());

}


void Test::PopStack()
{

   if(!Stack.empty() )
    {
      Stack.pop_back();

    } 

}

Option 1 选项1

Since Stack is used solely in the implementation of the member functions of Test , make that a private and static member variable of the class. 由于Stack仅用于Test成员函数的实现,因此请使其成为该类的privatestatic成员变量。

class Test
{
   public:
      void AddStack(std::string str);
      void PopStack();

   private:
         static std::vector<std::string> Stack;
};

and in a .cpp file, add: 在.cpp文件中,添加:

std::vector<std::string> Test::Stack;

Option 2 选项2

Make Stack available to the member functions of Test through a function call instead of a variable. 通过函数调用而不是变量使Test的成员函数可以使用Stack

class Test
{
   public:
      void AddStack(std::string str);
      void PopStack();

   private:
      static std::vector<std::string>& getStack();
};

Implementation: 执行:

std::vector<std::string>& getStack()
{
   static std::vector<std::string> Stack;
   return Stack;
}

void Test::AddStack(std::string str)
{
   auto& Stack = getStack();
   Stack.insert(Stack.end(),str.begin(),str.end());
}

void Test::PopStack()
{
   auto& Stack = getStack();
   if(!Stack.empty() )
   {
      Stack.pop_back();
   } 
}

Option 3 选项3

Make Test a singleton and make Stack a non- static member of the class. 使Test为单例,并使Stack为该类的非static成员。

class Test
{
   public:

      static Test& instance();
      void AddStack(std::string str);
      void PopStack();

   private:
      std::vector<std::string> Stack;
};

Implementation: 执行:

Test& Test::instance()
{
   static Test theInstance;
   return theInstance;
}

void Test::AddStack(std::string str)
{
   Stack.insert(Stack.end(),str.begin(),str.end());
}

void Test::PopStack()
{
   if(!Stack.empty() )
   {
      Stack.pop_back();
   } 
}

and use it as: 并将其用作:

Test::instance().AddStack("abcd");
Test::instance().PopStack();

you must be careful with static variables, I suggest to consider this first: 您必须谨慎使用静态变量,我建议您首先考虑以下因素:

  1. Dynamic memory allocation. 动态内存分配。 vectors have bad habit to realloc memory when need more space, and I'm not sure about the impact of dynamic memory allocation with static variables, because static variables are created in heap and you don't want fragmented memory there. 向量有在需要更多空间时重新分配内存的不良习惯,而且我不确定使用静态变量分配动态内存的影响,因为静态变量是在堆中创建的,并且您不希望在那儿分散内存。 The implementation will depends of the compiler, and probably, some fancy option for it. 该实现将取决于编译器,并且可能取决于它的一些理想选择。

  2. Classes functions related with that variable must be static too, because by definition static variables exist always, and function related must exist always too (easy way to explain). 与该变量相关的类函数也必须是静态的,因为根据定义,静态变量始终存在,而与函数相关的函数也必须始终存在(简单的解释方式)。 In Other words, static functions only have access to static variables for modifications, and that means a bunch of static functions. 换句话说,静态函数只能访问静态变量进行修改,这意味着一堆静态函数。

I suggest a nice and easy "export" variable, as I see, does the job without problems/restrictions of static variables. 我建议一个好用的“导出”变量,正如我所看到的那样,它可以完成工作而不会出现静态变量的问题/限制。

blabla.h blabla.h

export std::vector<std::string> my_no_static_vector;

blabla.cpp blabla.cpp

#include "blabla.h"
std::vector<std::string> my_no_static_vector;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM