要使用全局变量还是没有???? [英] To use global variables or no????
问题描述
很抱歉问另一个全局变量问题,但是从阅读其他
的帖子我还不确定是否使用它们。
我有一个带有set函数的程序,它可以调用
顺序中的4个其他函数 - 让我们说函数A,B,C,D。
它总是首先调用函数A,它是一个返回
系统路径的函数。现在所有其他函数也需要变量
(函数A返回一个char指针)
所以我的问题是 - 我应该声明一个全局变量,设置它的价值
使用函数A然后在其他函数中使用该变量
well。
或者是有一个首选的方法来实现我的需要吗?希望我已经明确了
!!
谢谢。
Sorry to ask another global variable question, but from reading other
posts I''m still not sure whether to use them or not.
I have a program with a set function that calls 4 other functions in
order - let''s say function A, B, C, D.
It always calls function A first which is a function that returns a
system path. Now all other functions require that variable as well
(function A returns a char pointer)
So my question is - should I declare a global variable, set it''s value
using function A and then use that variable in the other functions as
well.
Or is there a preferred method to achieve what I need? Hope I have made
myself clear!!
Thanks.
推荐答案
eoindeb写道:
eoindeb wrote:
很抱歉问另一个全局变量问题,但是从阅读其他
帖子我还是不确定是否要是否使用它们。
我有一个带有set函数的程序,它按照
顺序调用其他4个函数 - 让我们说函数A,B,C,D。 >
它总是先调用函数A,它是一个返回
系统路径的函数。现在所有其他函数也需要变量
(函数A返回一个char指针)
所以我的问题是 - 我应该声明一个全局变量,设置它的值
使用函数A然后在其他函数中使用该变量
或者是否有一种首选方法来实现我的需求?希望我已经做好了自己清楚!!
Sorry to ask another global variable question, but from reading other
posts I''m still not sure whether to use them or not.
I have a program with a set function that calls 4 other functions in
order - let''s say function A, B, C, D.
It always calls function A first which is a function that returns a
system path. Now all other functions require that variable as well
(function A returns a char pointer)
So my question is - should I declare a global variable, set it''s value
using function A and then use that variable in the other functions as
well.
Or is there a preferred method to achieve what I need? Hope I have made
myself clear!!
如果我理解正确,你有一个函数,比如F,调用
按顺序执行A,B,C和D功能。函数A为其他三个提供输入值
。我不明白为什么你应该担心全局(文件
范围)变量。只需在函数F中声明一个局部变量,并使用
它来收集A的返回值,并将其传递给B,C和D.这样的一个
变量是不是全局的,虽然它用于更多的功能。
尽管如此,使用函数外部的变量可能不是一个好主意,
看着函数调用它不是立即显而易见的是什么是输入和输出。你没有一个干净的界面,你需要引入(和使用)副作用。
一如既往,全局变量确实有它们的用途,但是必须根据具体情况判断
。我不认为你提供足够的
关于你的信息来做出好的猜测。
If I understand correctly, you have one function, say F, that calls
functions A, B, C, and D in order. Function A provides input value for
the other three. I don''t see why you should worry about global (file
scope) variables. Just declare a local variable in function F, and use
it to collect return value of A, and pass it to B, C, and D. Such a
variable is not global, although it is used in more functions.
Still, using variables external to the function may not be a good idea,
as looking at the function call it is not immediatelly obvious what are
the inputs, and outputs. You don''t have a clean interface, you
introduce (and use) side effects.
As always, global variables do have their uses, but that has to be
judged on a case by case basis. I don''t think you provide enough
information about yours to make a good guess.
eoindeb <,EO ***** @ gmail.com>在消息中写道
news:11 ********************* @ u72g2000cwu.googlegro ups.com ...
"eoindeb" <Eo*****@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
很抱歉问另一个全局变量问题,但是从阅读其他
帖子我还不确定是否使用它们。
我有一个带有set函数的程序在
顺序中调用其他4个函数 - 让我们说函数A,B,C,D。
它总是先调用函数A,它是一个返回
系统路径。现在所有其他函数也需要变量
(函数A返回一个char指针)
所以我的问题是 - 我应该声明一个全局变量,设置它的值
使用函数A然后在其他函数中使用该变量
或者是否有一种首选方法来实现我的需求?希望我已经把自己弄清楚!!
谢谢。
Sorry to ask another global variable question, but from reading other
posts I''m still not sure whether to use them or not.
I have a program with a set function that calls 4 other functions in
order - let''s say function A, B, C, D.
It always calls function A first which is a function that returns a
system path. Now all other functions require that variable as well
(function A returns a char pointer)
So my question is - should I declare a global variable, set it''s value
using function A and then use that variable in the other functions as
well.
Or is there a preferred method to achieve what I need? Hope I have made
myself clear!!
Thanks.
全局变量的一个问题是很难看到
这些变量会发生变化。例如:
char * str;
int n = 1;
void func1(){
str =(n == 1)? "你好" :bye;
fun2();
fun3();
fun4();
fun5();
/ *这里n和str的值是多少? * /
以上问题的答案是:我必须检查
fun2,fun3,fun4和fun5中的代码,因为我不知道<可能修改全局变量的
。
如果你有很多变量,这些变量对于几个函数来说很常见,你可以通过调用传递它们,或者你可以设置一个包含所有共享的
变量的结构,然后只传递该结构。
/ *定义结构MyStruct * /
....
然后:
void func1(){
struct MyStruct s;
/ *填写初始值* /
...
fun2();
fun3(& s);
fun4();
fun5(& s);
现在我知道我只需要检查fun3和fun5
来查找变量的可能变化。
-
Fred L. Kleinschmidt
波音助理技术研究员
技术支持chitect,软件重用项目
One problem with global variables is that it is difficult to see where
those variables get changed. For example:
char *str;
int n = 1;
void func1() {
str = (n==1)? "hello" : "bye";
fun2();
fun3();
fun4();
fun5();
/* what are the values of n and str here ? */
The answer to the above question is: i have to examine
the code in fun2, fun3, fun4, and fun5, because I don''t know
who might modify the global variables.
If you have a lot of variables that are common to several
of the functions, you can pass them all throught the call,
or you can set up a struct that contains all of the shared
variables, and just pass that struct around.
/* define struct MyStruct */
....
Then:
void func1() {
struct MyStruct s;
/* Fill in initial values */
...
fun2();
fun3(&s);
fun4();
fun5(&s);
Now I know that I only have to examine fun3 and fun5
to find possible changes to the variables.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
eoindeb说:
eoindeb said:
很抱歉问另一个全局变量问题,但是从阅读其他
帖我仍然不确定是否使用它们。
全局变量的第一条规则:不要使用它们。
全局变量的第二条规则(仅限专家):Don'不要使用它们*。
或者有一种首选的方法来实现我的需求吗?希望我已经做好了
自己清楚!!
Sorry to ask another global variable question, but from reading other
posts I''m still not sure whether to use them or not.
First Rule of Global Variables: Don''t use them.
Second Rule of Global Variables (for experts only): Don''t use them *yet*.
Or is there a preferred method to achieve what I need? Hope I have made
myself clear!!
参数。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)
Parameters.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
这篇关于要使用全局变量还是没有????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!