全局变量的最佳实践? [英] best practice for global variables?

查看:43
本文介绍了全局变量的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正在努力确保我使用最佳实践(并因此节省了一些

头痛)的声明和定义全局变量。让's $

说我有一个包含30个文件的应用程序,包括main.cpp我有全局

变量需要在main.cpp中定义并在所有其他文件中声明

in。我看到它完成的方式是在一个标题中定义/声明所有内容

文件(例如globalincludes.h)以EXTERN

然后在main.cpp中EXTERN是

#define EXTERN


和所有其他文件中的EXTERN是

#define EXTERN extern


所以除了在main之外,一切都是extern。但是,我似乎还需要

定义和声明。如果(在globalincludes.h中)我有变量


EXTERN int MonthEndDate [] = {31,28,31,30,31,30,31,31,30,31,30 ,

31};


这作为定义,但即使使用

extern关键字,我也会获得多次重新定义。我是否也需要单独的声明?


extern int MonthEndDate [];


我的globalincludes.h文件对于这个变量应该是什么样的?


谢谢!


Jay

解决方案

* Jay Wolfe:


我正在努力确保我使用全局的声明和定义来使用最佳实践(从而节省一些头痛)变量。


最佳做法是:不要使用全局变量。

[snip]我的globalincludes.h文件对于这个变量应该是什么样的?




最佳做法是:不要使用全局包含文件。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?




" Alf P 。斯坦巴赫 <人*** @ start.no>在消息中写道

news:49 ************ @ individual.net ...

* Jay Wolfe:


我正在努力确保我使用全局变量的声明和定义来使用最佳实践(从而节省一些头痛)。



[snip]

我的globalincludes.h文件对于这个变量应该是什么样的?



最佳做法是:不要使用全局包含文件。




谢谢,但那不是真的很很有帮助。你所做的就是告诉我,我不能做什么,但是没有提供解释为什么也没有其他选择。

最佳实践意味着什么是最好的做法,不是所有你不应该做的事情。

我有共同的数据,每个文件需要看到和全局变量似乎

就像是为此而做的。如果你有一个很好的解决方案,请告诉我。


Jay




Jay Wolfe写道:

" Alf P. Steinbach" <人*** @ start.no>在消息中写道
新闻:49 ************ @ individual.net ...

* Jay Wolfe:

<我正在努力确保我使用全局变量的声明和定义来使用最佳实践(从而节省一些头痛)。



最好的练习是:不要使用全局变量。

[snip]

我的globalincludes.h文件对于这个变量应该是什么样的?



最佳做法是:不要使用全局包含文件。



谢谢,但这并不是非常有用。你所做的就是告诉我,我不能做某事,但没有提供任何解释为什么也没有其他选择。
最佳实践意味着什么是最好的,而不是你应该做的所有事情。不行。
我有一些常见的数据,每个文件都需要看到,而全局变量似乎就像是为此而制作的。如果你有一个很好的解决方案,请告诉我。




总有办法避免全局变量。一种这样的方式是

单身。


全局如下:


header.h:


extern类型var [];


main.cpp

类型var [];


Hello,

I''m trying to make sure I use best practices (and hence save myself some
headaches) with the declaration and definition of global variables. Let''s
say I have an app with 30 files, including main.cpp I have global
variables that need defining in main.cpp and declaring in all other files
in. The way I''ve seen it done is to define/declare everything in one header
file (e.g. globalincludes.h) prefaced with the word EXTERN

Then in main.cpp EXTERN is
#define EXTERN

and in all other files EXTERN is
#define EXTERN extern

so everything is extern except in main. However, it still seems like I need
both a definition and declaration. If (in globalincludes.h) I have variable

EXTERN int MonthEndDate[] = {31, 28, 31, 30, 31, 30, 31, 31, 30 ,31, 30,
31};

This works as the definition, but I get multiple redefinitions even with the
extern keyword. Do I need separate declarations too?

extern int MonthEndDate[];

What should my globalincludes.h file look like for this one variable?

Thanks!

Jay

解决方案

* Jay Wolfe:


I''m trying to make sure I use best practices (and hence save myself some
headaches) with the declaration and definition of global variables.
The best practice is: don''t use global variables.
[snip] What should my globalincludes.h file look like for this one variable?



The best practice is: don''t use global include files.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



"Alf P. Steinbach" <al***@start.no> wrote in message
news:49************@individual.net...

* Jay Wolfe:


I''m trying to make sure I use best practices (and hence save myself some
headaches) with the declaration and definition of global variables.



The best practice is: don''t use global variables.

[snip]

What should my globalincludes.h file look like for this one variable?



The best practice is: don''t use global include files.



Thanks, but that''s not really very helpful. All you''ve done is tell me I
can''t do something, but offered no explanation why and no alternatives.
Best practice means what is best to do, not all the things you shouldn''t do.
I have common data that every file needs to see and global variables seems
like it was made for this. If you have a good solution, please let me know.

Jay



Jay Wolfe wrote:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:49************@individual.net...

* Jay Wolfe:


I''m trying to make sure I use best practices (and hence save myself some
headaches) with the declaration and definition of global variables.



The best practice is: don''t use global variables.

[snip]

What should my globalincludes.h file look like for this one variable?



The best practice is: don''t use global include files.



Thanks, but that''s not really very helpful. All you''ve done is tell me I
can''t do something, but offered no explanation why and no alternatives.
Best practice means what is best to do, not all the things you shouldn''t do.
I have common data that every file needs to see and global variables seems
like it was made for this. If you have a good solution, please let me know.



There is always a way to avoid global variables. One such way is a
singleton.

globals like so:

header.h:

extern type var[];

main.cpp
type var[];


这篇关于全局变量的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆