在头文件里定义static变量,若有多个文件包含了这个头文件,就会有多份该static变量?

weiwuyuan 2014-05-14 04:05:23
我建了个空项目,在一个头文件里写了个static 类对象,然后包含2次该头文件,构造函数跑了2次,包含1次,就只跑1次

PS:已经写了“#pragma once”指令,用宏也依旧如此,

这是什么原因?
...全文
1134 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
weiwuyuan 2014-05-30
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
static static declarator When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared). In C++, when modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all the instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members. For related information, see auto, extern, and register. Example // Example of the static keyword static int i; // Variable accessible only from this file static void func(); // Function accessible only from this file int max_so_far( int curr ) { static int biggest; // Variable whose value is retained // between each function call if( curr > biggest ) biggest = curr; return biggest; } // C++ only class SavingsAccount { public: static void setInterest( float newValue ) // Member function { currentRate = newValue; } // that accesses // only static // members private: char name[30]; float total; static float currentRate; // One copy of this member is // shared among all instances // of SavingsAccount }; // Static data members must be initialized at file scope, even // if private. float SavingsAccount::currentRate = 0.00154;
想跟大神您提个问题的是,可能并不是所有人,都有读英文的习惯,受偏好,习惯,以及能力的限制。。 不过,这可以作为我将来努力的目标。
weiwuyuan 2014-05-30
  • 打赏
  • 举报
回复
虽然说,static变量只对本文件有效,但我还是希望编译器能够阻止重名的static变量,否则这将是一个隐患,
weiwuyuan 2014-05-30
  • 打赏
  • 举报
回复
引用 10 楼 yunchao630 的回复:
cpp文件包含了那个头文件,相当于把头文件的代码,复制到cpp文件的上面。也就是在cpp里定义了static全局变量,只对当前文件有效。
恩,这我能理解, 我的抱怨是,我开始对此细节并不清楚,以为我定义了一个static对象,整个程序就只有一份,结果出问题了。。。。
默伊清风 2014-05-30
  • 打赏
  • 举报
回复
引用 3 楼 jmppok 的回复:
头文件里加上 #ifndef XX_H #define XX_H #endif //XX_H 可以防止一个头文件被多次包含
taodm 2014-05-30
  • 打赏
  • 举报
回复
其实,楼主,换本合格的教材补下基础吧。 C++坑多着呢。这个从C继承过来的语法点你都还不清楚,后面的路不好走啊。
weiwuyuan 2014-05-30
  • 打赏
  • 举报
回复
回楼上: 不要用明白人的眼光去审视不明白之人, 一个很难理解的东西,在已经了解之人的眼中是多么简单,但你们从来不知道,对于不明白之人,他们的理解是什么样, 不要将自己的意志强加给别人,我只能说,你们的眼光,角度,太狭隘了。 你以为你看懂了,你以为你贴的长篇大论已经给出了答案,那完全是因为你早就理解了这个概念,这段内容不过是给你提供了一个可靠的应证罢了。 简单的东西,就是这样被你们搞复杂了,本来一句话就可以解释好的东西,非得整个长篇大论。 很多人,对待一个问题,根本不知道如何用语言表达出来,尤其向不理解之人进行表达, 很多无谓的时间,也都花费在了这个上面, 其实,你们从一开始,就没有弄清人类认知的方方面面,狭隘至极!
fukainankai 2014-05-30
  • 打赏
  • 举报
回复
菜鸟来翻译一下哈,大神的话很有道理喔,而且似乎哪儿都能看到赵4老师的身影啊,首先向赵4老师的辛勤工作致敬。 静态 静态声明 当使用static关键字标记一个变量的时候,表示这个变量拥有静态的生命周期(当程序开始的时候,它会被分配,程序结束的时候,它会被释放释放),而且,它会被初始化为0,除非你指定了另一个值。当在文件作用域内标记一个变量或者函数的时候,表示这个变量或者函数拥有内部链接(这个名字不会在它被声明的文件外识别) 菜鸟译者注:本人的理解是,如果你用static标示了一个全局变量,其它文件中,是无法用extern来访问的。 在C++中,在一个类声明中,用static关键词标记一个数据成员的时候,标示这个变量的副本将会被所有这个类的实例共享。当在类声明中,用static关键字标示函数的时候,意味着这个函数只能访问静态成员。 更多的信息,请看auto extern和register。 代码就不翻译了吧。。 赵4老师的意思大概是说,这段文字你可以从msdn上看到,所以这种问题多看看文档要比在论坛里看的效果好,并且省时的多。我英语也很菜喔,不一定比你强,只不过我耐心看下去了而已,当然我也经常懒,不想看。。
引用 1 楼 zhao4zhong1 的回复:
static static declarator When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared). In C++, when modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all the instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members. For related information, see auto, extern, and register. Example // Example of the static keyword static int i; // Variable accessible only from this file static void func(); // Function accessible only from this file int max_so_far( int curr ) { static int biggest; // Variable whose value is retained // between each function call if( curr > biggest ) biggest = curr; return biggest; } // C++ only class SavingsAccount { public: static void setInterest( float newValue ) // Member function { currentRate = newValue; } // that accesses // only static // members private: char name[30]; float total; static float currentRate; // One copy of this member is // shared among all instances // of SavingsAccount }; // Static data members must be initialized at file scope, even // if private. float SavingsAccount::currentRate = 0.00154;
weiwuyuan 2014-05-30
  • 打赏
  • 举报
回复
引用 15 楼 zhao4zhong1 的回复:
何况还有“Google翻译”呢。
简单一句话就是: 若把static定义的变量放在头文件里,那么有多少个头文件包含了他,内存里就有多少份该static变量,而不是整个程序只有1份。 从您列的英文内容里,貌似找不到这样的描述。
赵4老师 2014-05-30
  • 打赏
  • 举报
回复
何况还有“Google翻译”呢。
赵4老师 2014-05-30
  • 打赏
  • 举报
回复
常用软件开发相关英文单词总数大概不超过2000个吧。
翅膀又硬了 2014-05-15
  • 打赏
  • 举报
回复
cpp文件包含了那个头文件,相当于把头文件的代码,复制到cpp文件的上面。也就是在cpp里定义了static全局变量,只对当前文件有效。
翅膀又硬了 2014-05-15
  • 打赏
  • 举报
回复
引用 7 楼 weiwuyuan 的回复:
[quote=引用 6 楼 dk385 的回复:] 在一个头文件中声明static 类对象, 会为所有include该头文件的cpp都创建一个同名的全局对象,只不过它们是相互独立的。 要避免这个问题,可以在cpp中定义变量, 然后在相应的关文件中加入extern 声明就可以。 如

abc.h: 
extern int  nAbc;
abc. cpp: 
int nAbc;
“#pragma once”指令只是告诉编译器这个关文件编译一次就好了,加快编译速度, 但并不是说就不用声明变量了,否则cpp文件中不知道去那找这些变量。 一般头文件中就是声明变量,然后变量的定义放在cpp文件中,如果在头文件有变量定义,那么当有多个cpp文件include了这个头文件时,就会出现重定义错误。 楼主如果还不明白,可以自己查下变量的声明和定义区别。
定义普通的变量的确是会出现重定义的错误(如果有2个或2个以上的cpp包含了的话),但为什么加了static,就不提示重复定义了呢,坑爹。[/quote]这是static全局变量的特性呀,只对当前文件有效。
weiwuyuan 2014-05-14
  • 打赏
  • 举报
回复
也就是说头文件只是被编译一次,但是会被包含多次。。就是每个cpp都有一份该头文件的拷贝
weiwuyuan 2014-05-14
  • 打赏
  • 举报
回复
引用 6 楼 dk385 的回复:
在一个头文件中声明static 类对象, 会为所有include该头文件的cpp都创建一个同名的全局对象,只不过它们是相互独立的。 要避免这个问题,可以在cpp中定义变量, 然后在相应的关文件中加入extern 声明就可以。 如

abc.h: 
extern int  nAbc;
abc. cpp: 
int nAbc;
“#pragma once”指令只是告诉编译器这个关文件编译一次就好了,加快编译速度, 但并不是说就不用声明变量了,否则cpp文件中不知道去那找这些变量。 一般头文件中就是声明变量,然后变量的定义放在cpp文件中,如果在头文件有变量定义,那么当有多个cpp文件include了这个头文件时,就会出现重定义错误。 楼主如果还不明白,可以自己查下变量的声明和定义区别。
定义普通的变量的确是会出现重定义的错误(如果有2个或2个以上的cpp包含了的话),但为什么加了static,就不提示重复定义了呢,坑爹。
家鸣 2014-05-14
  • 打赏
  • 举报
回复
在一个头文件中声明static 类对象, 会为所有include该头文件的cpp都创建一个同名的全局对象,只不过它们是相互独立的。 要避免这个问题,可以在cpp中定义变量, 然后在相应的关文件中加入extern 声明就可以。 如

abc.h: 
extern int  nAbc;
abc. cpp: 
int nAbc;
“#pragma once”指令只是告诉编译器这个关文件编译一次就好了,加快编译速度, 但并不是说就不用声明变量了,否则cpp文件中不知道去那找这些变量。 一般头文件中就是声明变量,然后变量的定义放在cpp文件中,如果在头文件有变量定义,那么当有多个cpp文件include了这个头文件时,就会出现重定义错误。 楼主如果还不明白,可以自己查下变量的声明和定义区别。
weiwuyuan 2014-05-14
  • 打赏
  • 举报
回复
楼上两位你们建个空项目,用#pragma message("xxx") 就能知道一个头文件会被包含几次了。。
翅膀又硬了 2014-05-14
  • 打赏
  • 举报
回复
在源文件里定义,头文件声明
jmppok 2014-05-14
  • 打赏
  • 举报
回复
头文件里加上 #ifndef XX_H #define XX_H #endif //XX_H 可以防止一个头文件被多次包含
weiwuyuan 2014-05-14
  • 打赏
  • 举报
回复
问题的根源貌似是:#pragma once 只是防止同一个cpp重复包含,而不是多个 就是如果有多个cpp包含了同一个头文件,那么该头文件会被包含多次。
赵4老师 2014-05-14
  • 打赏
  • 举报
回复
static static declarator When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared). In C++, when modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all the instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members. For related information, see auto, extern, and register. Example // Example of the static keyword static int i; // Variable accessible only from this file static void func(); // Function accessible only from this file int max_so_far( int curr ) { static int biggest; // Variable whose value is retained // between each function call if( curr > biggest ) biggest = curr; return biggest; } // C++ only class SavingsAccount { public: static void setInterest( float newValue ) // Member function { currentRate = newValue; } // that accesses // only static // members private: char name[30]; float total; static float currentRate; // One copy of this member is // shared among all instances // of SavingsAccount }; // Static data members must be initialized at file scope, even // if private. float SavingsAccount::currentRate = 0.00154;

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧