如何避免全局变量和extern。 [英] How to avoid global variables and extern.

查看:83
本文介绍了如何避免全局变量和extern。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临以下问题:我正在使用Gnu科学图书馆

(它是一个C数学库),特别是随机数生成

算法。示例代码是:


....

gsl_rng * r;

....

double u = gsl_rng_uniform(r);


问题是r代表随机数的迭代

生成器算法,因此每次需要时都需要访问

来生成一个新的随机数。在C中你可以通过以下方式解决问题:


1)将r传递给需要生成随机数的每个函数

(负担......)

2)将r定义为全局变量并在evey文件中使用extern(不是很好

优雅)


我是想知道是否有更好的方法来解决c ++中的问题。我还想在我的代码中将任何明显的依赖关系切换到

gsl。这促使创建了一个包装类,如:


class RandomNumberGenerator

{

private:

gsl_rng * r;


public:


RandomNumberGenerator();


double generateUniform( );


};


然后我希望有类似这个类的独特对象的东西

可以在任何地方使用,不需要启动(静态?)

是否有可能完成这项任务?

你有更好的解决方案吗?

提前感谢您的帮助。


StephQ

I''m facing the following problem: I''m using the Gnu Scientific Library
(it is a C math library), and in particular random number generation
algorithms. Example code is:

....
gsl_rng * r;
....
double u = gsl_rng_uniform (r);

The problem is that r "represent" the iteration of the random number
generator algorithm, so it needs to be accessible every time you need
to generate a new random number. In C you can solve the problem by:

1) passing r to every function that needs to generate random numbers
(a burden....)
2) define r as global variable and use extern in evey file (not very
elegant)

I''m wondering if there is a better way to solve the problem in c++. I
also would like to toggle any explicit dependence in my code to the
gsl. This prompted the creation of a wrapper class like:

class RandomNumberGenerator
{
private:
gsl_rng* r;

public:

RandomNumberGenerator() ;

double generateUniform();

};

Then I would like to have something like a unique object of this class
available everywhere that does not need to be initilized (static?)
Is it possible to accomplish this task?
Do you have some better solutions to propose?
Thank you in advance for any help.

StephQ

推荐答案



" StephQ" < as ******* @ mailinator.comwrote in message

news:11 ********************** @ s33g2000prh.googlegr oups.com ...

"StephQ" <as*******@mailinator.comwrote in message
news:11**********************@s33g2000prh.googlegr oups.com...

我正面临以下问题:我正在使用Gnu科学图书馆

(它是一个C数学库),特别是随机数生成

算法。示例代码是:


...

gsl_rng * r;

...

double u = gsl_rng_uniform(r);


问题是r代表随机数的迭代

生成器算法,因此每次需要时都需要访问

来生成一个新的随机数。在C中你可以通过以下方式解决问题:


1)将r传递给需要生成随机数的每个函数

(负担......)

2)将r定义为全局变量并在evey文件中使用extern(不是非常优雅的b $ b)
I''m facing the following problem: I''m using the Gnu Scientific Library
(it is a C math library), and in particular random number generation
algorithms. Example code is:

...
gsl_rng * r;
...
double u = gsl_rng_uniform (r);

The problem is that r "represent" the iteration of the random number
generator algorithm, so it needs to be accessible every time you need
to generate a new random number. In C you can solve the problem by:

1) passing r to every function that needs to generate random numbers
(a burden....)
2) define r as global variable and use extern in evey file (not very
elegant)



如何将r定义为全局变量,然后将其声明为

只是* one *头文件,并在任何需要的任何

实现文件中包含该头文件使用r?


(不好的名字,r,但是。更能描述其目的,而且更少

可能与名字发生冲突局部变量会更好。)


-Howard

How about defining r as a global variable, then declaring it as extern in
just *one* header file, and simply including that header in any
implementation file which needs to use r?

(Bad name, "r", though. Something more descriptive of its purpose, and less
likely to have a name clash with local variables, would be better.)

-Howard


2007年4月26日星期四18:11:54 GMT, "霍华德"写道:
On Thu, 26 Apr 2007 18:11:54 GMT, "Howard" wrote:

>" StephQ"写道:
>"StephQ" wrote:

> gsl_rng * r;
...
double u = gsl_rng_uniform(r);
问题是r代表随机数生成器算法的迭代,因此每次需要生成新的随机数时都需要访问它。在C中你可以通过以下方式解决问题:
1)将r传递给需要生成随机数的每个函数
(负担....)2)将r定义为全局变量在evey文件中使用extern(不是很优雅)
>gsl_rng * r;
...
double u = gsl_rng_uniform (r);

The problem is that r "represent" the iteration of the random number
generator algorithm, so it needs to be accessible every time you need
to generate a new random number. In C you can solve the problem by:
1) passing r to every function that needs to generate random numbers
(a burden....)
2) define r as global variable and use extern in evey file (not very
elegant)


如何将r定义为全局变量,然后将其声明为extern in
just * one * header文件,只需在任何需要使用r的实现文件中包含该头文件?


How about defining r as a global variable, then declaring it as extern in
just *one* header file, and simply including that header in any
implementation file which needs to use r?



非const全局变量有严重的缺点,通常不会被认为是好的风格。由于变量需要'表示

随机数生成器算法的迭代'我不认为

它'是'''负担将其传递给职能部门。

-

Roland Pibinger

最好的软件简单,优雅,充满了剧" - Grady Booch

Non-const global variables have severe drawbacks and are usually not
considered good style. Since the variable is needed to ''"represent"
the iteration of the random number generator algorithm'' I don''t think
it''s a ''burden'' to pass it to functions.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch


Roland Pibinger写道:
Roland Pibinger wrote:

2007年4月26日星期四18:11:54 GMT ,霍华德写道:
On Thu, 26 Apr 2007 18:11:54 GMT, "Howard" wrote:

>" StephQ"写道:
>"StephQ" wrote:

>> gsl_rng * r;
...
double u = gsl_rng_uniform(r);
<问题是r代表随机数生成器算法的迭代,因此每次需要生成新的随机数时都需要访问它。在C中你可以通过以下方式解决问题:
1)将r传递给需要生成随机数的每个函数
(负担....)2)将r定义为全局变量在evey文件中使用extern(不是很优雅)
>>gsl_rng * r;
...
double u = gsl_rng_uniform (r);

The problem is that r "represent" the iteration of the random number
generator algorithm, so it needs to be accessible every time you need
to generate a new random number. In C you can solve the problem by:
1) passing r to every function that needs to generate random numbers
(a burden....)
2) define r as global variable and use extern in evey file (not very
elegant)


如何将r定义为全局变量,然后将其声明为extern in
just * one * header文件,只需在任何需要使用r的实现文件中包含该头文件?

How about defining r as a global variable, then declaring it as extern in
just *one* header file, and simply including that header in any
implementation file which needs to use r?



非const全局变量有严重的缺点,通常不会被认为是好的风格。由于变量需要'表示

随机数生成器算法的迭代'我不认为

它'是'''负担将其传递给职能部门。


Non-const global variables have severe drawbacks and are usually not
considered good style. Since the variable is needed to ''"represent"
the iteration of the random number generator algorithm'' I don''t think
it''s a ''burden'' to pass it to functions.



我是第二个。


对于应用程序中的课程等级,我通常在环境周围练习传递

。包含资源的类,例如

首选项管理器,日志记录管理器以及其他类似的东西。


在奥地利C ++ alpha中( http://netcabletv.org/public_releases/

是一个环境(at_env .h)模板就是这样做的。


I second that.

For classes at the "application" level, I usually practice passing
around an "environment" class that contains resources such as a
preferences manager, logging manager and anything else like this.

In the Austria C++ alpha (http://netcabletv.org/public_releases/) there
is an Environment (at_env.h) template which does just that.


这篇关于如何避免全局变量和extern。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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