简体   繁体   English

删除c ++程序中的全局变量

[英]remove global variables in c++ program

I have a large program in c++ having too many function and global variables. 我有一个c ++的大程序,它有太多的函数和全局变量。 Now, I want to remove those global variables and I want to use only local variables rather than global variables. 现在,我想删除那些全局变量,我想只使用局部变量而不是全局变量。 Can any one suggest me the best way to do this. 任何人都可以建议我这样做的最好方法。 Thanks! 谢谢!

Its around 4000 lines program in C++. 它在C ++中的大约4000行程序。 There are 10 classes and 60 functions in those different classes, 30 global variables. 在这些不同的类中有10个类和60个函数,30个全局变量。 The global variables have used between functions of different classes. 全局变量用于不同类的函数之间。

I normally start by putting the global variables in a namespace 我通常首先将全局变量放在命名空间中

namespace global
{
  int var;
  ...
}

this will cause you to get errors everwhere that 'var' is used, then i would just replace the references one by one to the namespace variant. 这将导致您在使用'var'的地方获得错误,然后我只是将引用逐个替换为命名空间变体。 once all global variable references are in the namespace it is easier to see which variables are local and which are global 一旦所有全局变量引用都在命名空间中,就更容易看到哪些变量是本地变量,哪些变量是全局变量

if ( global::var == 1 ) ...

now by searching global::var you get a list of variables where it is used then you would need to go through the usage case by case to see if a variable is used in several modules or not, in those cases you may need to pass it in as a function argument in other cases declare it is a local variable. 现在通过搜索global::var你得到一个使用它的变量列表然后你需要逐个查看用法,看看是否在一些模块中使用了一个变量,在那些情况下你可能需要通过它作为函数参数在其他情况下声明它是一个局部变量。

its a cumbersome method but eliminating global vars is always worth doing. 这是一个繁琐的方法,但消除全球变量总是值得做的。

I suggest a solution such as removing their declarations from global scope and put them in the appreciated functions. 我建议一个解决方案,例如从全局范围中删除它们的声明,并将它们放在赞赏的函数中。

Examlpe: Examlpe:

int hello; // global
// remove it

Now, put it in the function: 现在,把它放在函数中:

void func()
{
   int hello; // local
  //...

We have two cases: 我们有两种情况:

If some other function needs to know the value of hello , let it take that hello as a parameter: 如果某些其他函数需要知道hello的值,那么让它作为参数将hello

void func2(const int &helloVar)
{
   // ...

If a global variable is used only by a single function, you can use a static local variable instead.. 如果全局变量仅由单个函数使用,则可以使用static局部变量。

I would suggest using a class. 我建议使用一堂课。 They are perfect for hiding variables and functions into one group and categorizing. 它们非常适合将变量和函数隐藏到一个组中并进行分类。 Here is the basic structure: 这是基本结构:

class example
{
public:
    example(); //basic constructor
    int function1();  //example function
    int x;  // a variable accesible globally
private:
    int y;  // a variable only accesible by functions of the class
};

Calling each function is easy but I wont explain since I dont want to start a text wall. 调用每个函数很容易,但我不会解释因为我不想启动文本墙。 You can go to this link for more information: http://www.cplusplus.com/doc/tutorial/classes/ 您可以访问此链接以获取更多信息: http//www.cplusplus.com/doc/tutorial/classes/

你可以把那些变量放在aclass中,然后在第一个位置创建指向该类对象的指针,然后使用那些变量然后将这个指针传递给每个需要那些变量的方法,不要忘记在不使用时删除对象。

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

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