关于extern的问题?.. [英] question about extern ?..

查看:81
本文介绍了关于extern的问题?..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

linux上的编译器g ++

file.h有以下内容,int x = 5;

并且在主程序文件中我可以访问 X"没有声明它

extern x。程序编译并运行正常....


程序编译并运行正常,x声明为extern。


所以有必要如果没有

声明它是extern,那么将x声明为extern?什么是标准和良好做法?

compiler g++ on linux

file.h has the following, int x=5;
and inside the main program file I can access "x" without declaring it
"extern x". Program compiles and runs fine....

also program compiles and runs fine with x declared as extern.

So is it necessary to declare x as extern if one get away without
declaring it extern ? What is the standard and good practice ?

推荐答案

mark写道:
mark wrote:
linux上的编译器g ++ file.h具有以下内容,int x = 5;
并且在主程序文件中我可以访问x和x。没有声明它
extern x。程序编译并运行良好....


这是因为你的''file.h''''#include'只用了一个翻译

单位。如果您尝试将其包含在多个翻译单元中(并且

,那就是'。''文件的用途),您最终会得到多个

''''的定义。由于ODR违反
违规(通常在链接阶段报告),您的程序将不再编译。

程序编译并运行正常,x声明为extern。


究竟是什么意思?这个


extern int x = 5;


?这也是一个定义。在命名空间范围内,这与

原始相同

int x = 5;


我所说的一切上面仍然适用。

所以如果没有
声明它是extern,有必要将x声明为extern吗?什么是标准和良好做法?
compiler g++ on linux

file.h has the following, int x=5;
and inside the main program file I can access "x" without declaring it
"extern x". Program compiles and runs fine....
That''s because your ''file.h'' is ''#include''d into exactly one translation
unit. If you try including it into more than one translation unit (and
that''s pretty much what ''.h'' files are for), you''ll end up with multiple
definitions of ''x''. Your program will no longer compile due to ODR
violation (usually reported at linking stage).
also program compiles and runs fine with x declared as extern.
What exactly do you mean? This

extern int x = 5;

? This is also a definition. In namespace scope this is the same as the
original

int x = 5;

All I said above still applies.
So is it necessary to declare x as extern if one get away without
declaring it extern ? What is the standard and good practice ?




好​​的做法是将''x'的非定义声明放入

头文件


extern int x; //注意:这里没有初始化程序


并将定义放入一个(并且只有一个)实现文件中


int x = 5 ;


当然,另一个好的做法是避免全局变量,除非

它们确实是必要的。


-

祝你好运,

Andrey Tarasevich



The good practice is to put a non-defining declaration of ''x'' into the
header file

extern int x; // Note: no initializer here

and put the definition into one (and only one) of the implementation files

int x = 5;

Of course, another good practice is to avoid global variables unless
they are really necessary.

--
Best regards,
Andrey Tarasevich


让我问这个简单的问题。


在哪里放一个extern int x声明?

(a)在标题(.h)文件中

(b)或程序文件(.cpp)
let me ask this simple question.

where does one put the "extern int x" statement ?
(a) In the header (.h) file
(b) or the program file (.cpp)


2004年6月8日05:09:26 -0700, ma ********* @ yahoo.com (mark)写道:
On 8 Jun 2004 05:09:26 -0700, ma*********@yahoo.com (mark) wrote:
让我问这个简单的问题。

在哪里放一个extern int x ;声明?
(a)在标题(.h)文件中
(b)或程序文件(.cpp)
let me ask this simple question.

where does one put the "extern int x" statement ?
(a) In the header (.h) file
(b) or the program file (.cpp)




作为Andrey说,把extern int x;在.h文件中,

*和*" int x = 5;"在* .cpp文件的* *中。


-

Andre Heinen

我的地址是a dot heinen在europeanlink dot com"



As Andrey said, put "extern int x;" in the .h file,
*and* "int x = 5;" in *one* of the .cpp files.

--
Andre Heinen
My address is "a dot heinen at europeanlink dot com"


这篇关于关于extern的问题?..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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