类中的静态成员 [英] Static member in a class

查看:76
本文介绍了类中的静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从c ++网站上读到以下句子,但不能理解为什么。任何人都可以帮我吗?


"

调试或实施

计划时要记住的一个重要细节使用静态类成员是因为你无法在类中初始化

静态类成员。实际上,如果您决定将代码放在头文件中,则甚至无法初始化头文件中的静态

变量;改为在.cpp文件中执行。


"

I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?

"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.

"

推荐答案

stonny写道:
stonny wrote:

我从c ++网站上读到以下句子,但不能理解为什么。任何人都可以帮我吗?


"

调试或实施

计划时要记住的一个重要细节使用静态类成员是因为你无法在类中初始化

静态类成员。实际上,如果您决定将代码放在头文件中,则甚至无法初始化头文件中的静态

变量;改为在.cpp文件中。


"
I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?

"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.

"



标准要求每个静态数据成员都在

命名空间级别定义。为了符合One Definition规则,如果将静态

数据成员的定义放在.cpp文件中(而不是.cpp文件),则
更有可能成功一个标题,可以包含在多个翻译单元中的
)。初始化伴随着

定义。这就是为什么你应该在

a .cpp文件中初始化静态数据成员(并且只在一个.cpp文件中)。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要'不要问

The Standard requires that every static data member is defined at the
namespace level. In order to comply with the One Definition rule, you
are more likely to succeed if you place the definition of the static
data member in a .cpp file (instead of a header which can be included
in more than one translation unit). Initialisation accompanies the
definition. That''s why you should initialise static data members in
a .cpp file (and only in one .cpp file).

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


* Victor Bazarov:
* Victor Bazarov:

stonny写道:
stonny wrote:

>我从c ++网站上读到了以下句子,但无法理解为什么。任何人都可以帮助我吗?

>
使用静态类成员调试或实现
程序时要记住的一个重要细节是你不能初始化
类内部的静态类成员。实际上,如果您决定将代码放在头文件中,则甚至无法在头文件中初始化静态
变量;请改为.cpp文件。

"
>I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?

"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.

"



标准要求每个静态数据成员都在

命名空间级别定义。为了符合One Definition规则,如果将静态

数据成员的定义放在.cpp文件中(而不是.cpp文件),则
更有可能成功一个标题,可以包含在多个翻译单元中的
)。初始化伴随着

定义。这就是为什么你应该在

a .cpp文件中初始化静态数据成员(并且只在一个.cpp文件中)。


The Standard requires that every static data member is defined at the
namespace level. In order to comply with the One Definition rule, you
are more likely to succeed if you place the definition of the static
data member in a .cpp file (instead of a header which can be included
in more than one translation unit). Initialisation accompanies the
definition. That''s why you should initialise static data members in
a .cpp file (and only in one .cpp file).



初始化伴随着常量的定义,除非在一个类中提供静态常量的声明提供



初始化程序,在这种情况下定义(如果有的话,然后在

类之外)是sans初始化程序。


示例:


struct S

{

static int const x = 42; //不是定义,根据§9.4.2/ 2。

};


如果使用S :: x的地址,或者在标准的术语,如果

S :: x被使用,则正式定义(但当前编译器不是必须在实践中使用
),在外面上课:


int const S :: x; //一个定义,根据§9.4.2/ 4。


不是针对你的,而是针对其他读者:这个结构只有

支持积分类型和/或包括枚举类型。


针对任何想到这个疯狂计划的人:呃。


-

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

问:为什么这么糟糕?

A:热门发布。

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

Initialization accompanies definition of a constant except in the case
when a declaration of a static constant in a class supplies the
initializer, in which case the definition (if any, and then outside the
class) is sans initializer.

Example:

struct S
{
static int const x = 42; // Not a definition, per §9.4.2/2.
};

If the address of S::x is used, or in the standard''s terminology, if
S::x is "used", a definition is formally (but with current compilers not
necessarily in practice) required, outside the class:

int const S::x; // A definition, per §9.4.2/4.

Not directed at you, but at other readers: this construct is only
supported for integral types and/including enum types.

Directed at whoever thought up this crazy scheme: ugh.

--
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?


5月8日下午4:06,stonny< zhangdexin2 ... @ gmail.comwrote:
On May 8, 4:06 pm, stonny <zhangdexin2...@gmail.comwrote:

我从c ++网站上读到以下句子,但不能理解为什么。任何人都可以帮我吗?
I read the following sentence from a c++ website, but can not
understand why. can anyone help me with it?


"

调试或实施

时要记住的一个重要细节使用静态类成员的程序是你不能初始化类里面的

静态类成员。实际上,如果您决定将代码放在头文件中,则甚至无法初始化头文件中的静态

变量;改为在.cpp文件中。


"
"
An important detail to keep in mind when debugging or implementing a
program using a static class member is that you cannot initialize the
static class member inside of the class. In fact, if you decide to put
your code in a header file, you cannot even initialize the static
variable inside of the header file; do it in a .cpp file instead.

"



静态类数据成员不是任何特定类的成员

实例。与该类的其他成员不同,该类中的

声明不是定义,因此您需要在其他地方定义




-

James Kanze(Gabi Software)电子邮件: ja ***** ****@gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard, 78210 St.-Cyr-l''cocole,法国,+ 33(0)1 30 23 00 34

Static class data members aren''t members of any particular class
instance. Unlike the other members of the class, the
declaration in the class is not a definition, so you need a
definition elsewhere.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于类中的静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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